我设置了10套警报,当应用程序被置于OnDestroy方法中时,我已经取消了所有警报,但仍有一些警报是活动的。我希望在应用程序被杀死时销毁所有警报。
public class MainActivity extends Activity {
int id;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(id=0;id<10;id++)
{
Long time = new GregorianCalendar()
.getTimeInMillis()
+10000;
Intent intentAlarm = new Intent(MainActivity.this,
AlarmReciever.class);
intentAlarm.putExtra("AlarmID", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, id, intentAlarm,
Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time,
PendingIntent.getBroadcast(
MainActivity.this, id,
intentAlarm,
PendingIntent.FLAG_UPDATE_CURRENT));
}
}
protected void onDestroy() {
for(id=0;id<10;id++)
{
Intent AlarmIntent = new Intent(MainActivity.this,
AlarmReciever.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(
MainActivity.this, id, AlarmIntent, 0);
alarmManager.cancel(pendingIntent);
}
super.onDestroy();
}
答案 0 :(得分:0)
嘿,要取消所有闹钟,请在onDestroy()方法中使用以下代码段:
Intent i = new Intent(getApplicationContext(), AlarmReciever.class);
AlarmActivity.pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, i,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)context. getSystemService(Context.ALARM_SERVICE);
am.cancel(AlarmActivity.pendingIntent);
为了取消所有警报,在循环变量的id = 1到10
的某个循环中运行上面的代码