I am a beginner(apology in advance if question is too novice). I am trying to pass integer value of the "startHour" and "startMinute" that I get in the following class to my Receiver class and change the phoen setting to Vibration for the required time.
But while running when I try to run nothing happens, no change in the audio settings of the phone.
Main Class
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
switch(v.getId()){
case R.id.startTime :
// Process to get Current Time
startHour = c.get(Calendar.HOUR_OF_DAY);
startMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd1 = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
eStart.setText(hourOfDay + ":" + minute);
}
}, startHour, startMinute, false);
tpd1.show();
break;
case R.id.stopTime :
// Process to get Current Time
stopHour = c.get(Calendar.HOUR_OF_DAY);
stopMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog tpd2 = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// Display Selected time in textbox
eStop.setText(hourOfDay + ":" + minute);
}
}, stopHour, stopMinute, false);
tpd2.show();
break;
Receiver Class
public class AlarmReceiver extends BroadcastReceiver {
AudioManager audioControl;
Bundle extras;
int startHour,startMinute,stopHour,stopMinute;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String day = sdf.format(d);
Calendar c = Calendar.getInstance();
audioControl = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
extras = intent.getExtras();
if(extras!=null){
startHour = extras.getInt("startHour");
startMinute = extras.getInt("startMinute");
stopHour = extras.getInt("stopHour");
stopMinute = extras.getInt("stopMinute");
}
int minute = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR_OF_DAY);
if(day.equals("Monday")){
if(hour>=startHour && hour<=stopHour){
if(hour==startHour && minute>=startMinute){
audioControl.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
else if(hour>startHour && hour<=stopHour){
if(hour==stopHour && minute>stopMinute){
audioControl.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
else{
audioControl.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
}
}
}
}
Also once I press the back button from my main class screen the value of startTime and stopTime is erased.How can I store it so that the Receiver function can use it.
Aucun commentaire:
Enregistrer un commentaire