gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:43:31 26 4
gpt4 key购买 nike

我有这个...

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

请告诉我哪里出了问题!!!谢谢!!

最佳答案

如果您保存对未决 Intent 的引用,那么您可以只使用 AlarmManager.cancel() .

传递您的待处理 Intent ,一切就绪。

关于android - 设置为重复时如何取消 PendingIntent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16467917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com