我有一个方法可以设置我想要发出警报的时间。 在这种方法中,我还有一个取消警报的停止按钮
if(alarmManager != null){
alarmManager.cancel(pi);
}
我的问题是,当我设置闹钟时,请退出应用程序并再次取消闹钟我得到一个nullPointer。我猜这是因为当我离开应用程序时,PendingIntent也会关闭(设置为null)。
如何防止这种情况发生,以便取消闹钟?
以下是整个方法:
@SuppressWarnings("deprecation")
public void setTime(){
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener()
{
int callCount = 0; //To track number of calls to onTimeSet()
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute)
{
if(callCount == 1) // On second call
{
String timeString = "";
timeString = selectedHour + ":" + selectedMinute + ":00";
Log.d("TEST", "Chosen time : "+ timeString);
setAlarm(timePicker, selectedHour, selectedMinute);
}
callCount++; // Incrementing call count.
}
}, hour, minute, true);
mTimePicker.setButton(DialogInterface.BUTTON_POSITIVE, "Set", mTimePicker);
mTimePicker.setButton(DialogInterface.BUTTON_NEGATIVE, "Stop", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(alarmManager != null){
alarmManager.cancel(pi);
}
}
});
mTimePicker.setTitle(R.string.time);
mTimePicker.show();
}
答案 0 :(得分:0)
您可以保存用于在Bundle中构建PendingIntent的数据,如here所示
因此,当您再次打开应用程序时,您将能够重建PendingIntent并在需要时取消警报。
编辑:首先,您必须覆盖onSaveInstanceState
并保存用于创建PendingIntent对象的数据,如下所示:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the pending intent id
savedInstanceState.putInt(PENDING_INTENT_ID, mPendingIntentId);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
之后,在onCreate
方法中,您将检查savedInstanceState是否为空。如果不为null,则从savedInstanceState(Bundle)对象中恢复项目,如下所示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mPendingIntentId = savedInstanceState.getInt(PENDING_INTENT_ID);
} else {
// Probably initialize members with default values for a new instance
mPendingIntentId = YOUR_DEFAULT_INT_VALUE;
}
mPendingIntent = PendingIntent.getBroadcast(this, mPendingIntentId, YOUR_INTENT, PendindIntent.FLAG_UPDATE_CURRENT);
}
每次进入应用程序时都会重新创建PendingIntent,这样您就可以取消上一步触发的相同警报。
编辑2:在你的情况下,另一种方法是,因为你只有一个警报,并且它的id总是相同的(应该用于PendingIntent的requestId),你可以只要您想添加或取消警报,只需重新创建PendingIntent即可。像下面这样的方法会帮助你。
private PendingIntent getPendingIntent(Context context){
return PendingIntent.getBroadcast(context, 0, new Intent(context, YourBroadcastReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT);
}