【问题标题】:How to cancel a PendingIntent when is set to repeating?设置为重复时如何取消 PendingIntent?
【发布时间】:2013-05-09 17:48:04
【问题描述】:

我有这个……

button = (Button) findViewById(R.id.start_repeating);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Test.this, RepeatingAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 1 * 1000;
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 1 * 1000, sender);
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(Test.this, "repeating_scheduled", Toast.LENGTH_LONG).show();
    }
});

button = (Button) findViewById(R.id.stop_repeating);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent intent = new Intent(Test.this, RepeatingAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(Test.this, 0, intent, 0);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.cancel(sender);
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(Test.this, "repeating_unscheduled", Toast.LENGTH_LONG).show();
    }
});

但它似乎无法正常工作...每次我尝试单击第二个按钮时,警报都不会取消...这是我正在使用的 BroadcastReceiver...

public class RepeatingAlarm extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        for(int i=0;i<5;i++)
            Toast.makeText(context, "repeating_received " + i, Toast.LENGTH_SHORT).show();
    }
}

请有人告诉我什么是错的!谢谢!!

【问题讨论】:

    标签: android alarmmanager android-pendingintent


    【解决方案1】:

    如果您保存对待处理意图的引用,则可以使用 AlarmManager.cancel()

    传入您的待处理意图,一切就绪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 2012-04-07
      • 2012-07-08
      • 2014-03-25
      • 2016-07-10
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      相关资源
      最近更新 更多