我的代码已经运行,但它有问题。我怎么解决它。 当我在timepickerdialog中第一次选择时(单击textview时间)。我按OK,按下切换按钮后,alarmmanager工作正常。我关闭了我的应用程序。然后再打开。我单击textview时间来显示我的timepickerdialog但我不选择另一个时间并单击取消。按下切换按钮,出现问题。我认为因为timepicker的值为null。我尝试了很多方法来解决它,但我不能。请帮帮我。这是我的代码 TextView显示TimePickerDialog。
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
timePicker.setIs24HourView(true);
calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);
calendar.set(Calendar.SECOND, 0);
get_hour = String.valueOf(selectedHour);
get_min = String.valueOf(selectedMinute);
if(selectedHour < 10 ){
get_hour = "0" + String.valueOf(selectedHour);
}
if(selectedMinute < 10) {
get_min = "0" + String.valueOf(selectedMinute);
}
millis = calendar.getTimeInMillis();
time.setText( get_hour + " : " + get_min);
savePrefs();
}
}, hour, minute, true);
mTimePicker.setTitle("Pick Time");
onoff_alarm.setChecked(false);
mTimePicker.show();
}
});
这是我的切换按钮。
onoff_alarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(onoff_alarm.isChecked()) {
if (get_hour.equals(null) && get_min.equals(null)){
Toast.makeText(getApplicationContext(), "Choose Another Time Please !", Toast.LENGTH_SHORT).show();
onoff_alarm.setChecked(false);
}else {
my_intent.putExtra("extra", "alarm on");
Toast.makeText(getApplicationContext(), " Set time when " + get_hour + " Hour " + get_min + " Min ", Toast.LENGTH_SHORT).show();
pending_intent = PendingIntent.getBroadcast(MainActivity.this, 0, my_intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, millis, AlarmManager.INTERVAL_DAY, pending_intent);
}
}
else {
my_intent.putExtra("extra","alarm off");
Toast.makeText(getApplicationContext(), "Cancel set time", Toast.LENGTH_SHORT).show();
alarmManager.cancel(pending_intent);
sendBroadcast(my_intent);
}
savePrefs();
}
});
}
我使用过像这样的SharePreferences:
public void savePrefs(){
SharedPreferences sharepreferences = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
String hour_set = String.valueOf(get_hour);
String min_set = String.valueOf(get_min);
SharedPreferences.Editor editor = sharepreferences.edit();
editor.putString(Hoursave, hour_set);
editor.putString(Minsave, min_set);
editor.commit();
public void loadPrefs(){
SharedPreferences sharepreferences = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
String hour_x = sharepreferences.getString(Hoursave, "");
String min_x = sharepreferences.getString(Minsave, "");
time.setText(hour_x + " : " + min_x);
答案 0 :(得分:0)
当您“关闭”应用程序时,将从系统中删除待处理的警报。您可以通过adb中的以下命令检查 -
adb shell dumpsys alarm
要解决此问题,请在设置警报时将警报时间存储在SharedPreferences中(最简单的方法,但如果您已经在使用DB,也可以选择数据库表)。接下来,当从已杀死状态重新打开应用程序时,只需通过从SharedPreferences(或DB)读取时间并使用相同的警报ID再次创建警报。