首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置为重复时如何取消PendingIntent?

设置为重复时如何取消PendingIntent?
EN

Stack Overflow用户
提问于 2013-05-10 01:48:05
回答 1查看 1.4K关注 0票数 1

我有这个..。

代码语言:javascript
复制
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 ..。

代码语言:javascript
复制
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();
    }
}

请谁告诉我出了什么问题!谢谢!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-10 02:23:59

如果您保存了对您的待定意图的引用,那么您可以只使用AlarmManager.cancel()

传入你的待定意图,一切都准备好了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16467917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档