1

我有一种方法可以设置我想要触发警报的时间。在这种方法中,我还有一个取消警报的停止按钮

                    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();
}
4

1 回答 1

0

您可以将用于构建 PendingIntent 的数据保存在 Bundle 中,如下所示

因此,当您再次打开您的应用程序时,您将能够重建 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 是否不为空。如果不为空,则从 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);
}
于 2014-10-23T10:27:30.973 回答