简体   繁体   English

这个 PendingIntent 警报怎么能取消?

[英]How could this PendingIntent alarm cancelled?

I've managed to understand this code.我已经设法理解这段代码。 This code is used in an reminder app.此代码用于提醒应用程序。 What this code do is to set and cancel alarm.这段代码的作用是设置和取消闹钟。 However, i don't know how the newly created reference could cancel existing PendingIntent.但是,我不知道新创建的引用如何取消现有的 PendingIntent。 Please explain me this code (the execute method).请向我解释这段代码(execute 方法)。

Here's the code:这是代码:

public class AlarmService extends IntentService {

    private static final String TAG = "AlarmService";

    public static final String POPULATE = "POPULATE";
    public static final String CREATE = "CREATE";
    public static final String CANCEL = "CANCEL";

    private IntentFilter matcher;

    public AlarmService() {
        super(TAG);
        matcher = new IntentFilter();
        matcher.addAction(POPULATE);
        matcher.addAction(CREATE);
        matcher.addAction(CANCEL);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getAction();
        String alarmId = intent.getStringExtra(AlarmMsg.COL_ALARMID);
        String alarmMsgId = intent.getStringExtra(AlarmMsg.COL_ID);
        String startTime = intent.getStringExtra(Alarm.COL_FROMDATE);
        String endTime = intent.getStringExtra(Alarm.COL_TODATE);

        if (matcher.matchAction(action)) {
            if (POPULATE.equals(action)) {
                RemindMe.dbHelper.populate(Long.parseLong(alarmId), RemindMe.db);
                execute(CREATE, alarmId);
            }

            if (CREATE.equals(action)) {
                execute(CREATE, alarmId, alarmMsgId, startTime, endTime);
            }

            if (CANCEL.equals(action)) {
                execute(CANCEL, alarmId, alarmMsgId, startTime, endTime);
                RemindMe.dbHelper.shred(RemindMe.db);
            }           
        }
    }

    /**
     * @param action
     * @param args {alarmId, alarmMsgId, startTime, endTime}
     */ 
    private void execute(String action, String... args) {       
        Intent i;
        PendingIntent pi;               
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Cursor c;

        String alarmId = (args!=null && args.length>0) ? args[0] : null;
        String alarmMsgId = (args!=null && args.length>1) ? args[1] : null;
        String startTime = (args!=null && args.length>2) ? args[2] : null;
        String endTime = (args!=null && args.length>3) ? args[3] : null;

        String status = CANCEL.equals(action) ? AlarmMsg.CANCELLED : AlarmMsg.ACTIVE;

        if (alarmMsgId != null) {
            c = RemindMe.db.query(AlarmMsg.TABLE_NAME, null, AlarmMsg.COL_ID+" = ?", new String[]{alarmMsgId}, null, null, null);

        } else {
            c = AlarmMsg.list(RemindMe.db, alarmId, startTime, endTime, status);
        }

        if (c.moveToFirst()) {
            long now = System.currentTimeMillis();
            long time, diff;
//          long count = c.getCount();
//          double MAX = alarmId!=null && count>30 ? Util.MONTH : Util.YEAR; 
            do {
                i = new Intent(this, AlarmReceiver.class);
                i.putExtra(AlarmMsg.COL_ID, c.getLong(c.getColumnIndex(AlarmMsg.COL_ID)));
                i.putExtra(AlarmMsg.COL_ALARMID, c.getLong(c.getColumnIndex(AlarmMsg.COL_ALARMID)));

                pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
//              pi = PendingIntent.getService(context, requestCode, intent, flags);

                time = c.getLong(c.getColumnIndex(AlarmMsg.COL_DATETIME));
                diff = time-now + (long)Util.MIN;
                if (CREATE.equals(action)) {
                    if (diff > 0 && diff < Util.YEAR)
                        am.set(AlarmManager.RTC_WAKEUP, time, pi);
                    //am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, interval, operation);

                } else if (CANCEL.equals(action)) {
                    am.cancel(pi);
                }                       
            } while(c.moveToNext());
        }
        c.close();      
    }

}

Alarm Manager is a SystemService .警报管理器是一个 SystemService 。 You can access it from any context and if you have the pending intent from which you started the Alarm.您可以从任何上下文访问它,如果您有启动警报的未决意图。 You can Cancel it from that pendingIntent.你可以从那个pendingIntent 取消它。 Just take a look at the documentation Link to Document只需看一下文档链接到文档

Hope it helps.希望能帮助到你。

This code cancels the alarm like this:此代码取消警报,如下所示:

            } else if (CANCEL.equals(action)) {
                am.cancel(pi);

This doesn't cancel the PendingIntent , it just cancels the alarm.这不会取消PendingIntent ,它只是取消警报。

This code will never set more than one alarm, because attempts to set a second alarm will automatically cancel the first one.此代码永远不会设置多个闹钟,因为尝试设置第二个闹钟将自动取消第一个闹钟。 This is because the same PendingIntent is used every time.这是因为每次都使用相同的PendingIntent

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警报应用程序不起作用在未决意图中显示错误 - alarm app not working shows error in pendingintent 出于警报管理器的目的,未决意图对于应用程序或设备是否具有唯一的ID? - For purposes of Alarm Manager, does a pendingintent have unique id for app or for device? Android AlarmManager.cancel()并未实际取消警报/ PendingIntent - Android AlarmManager.cancel() not actually cancelling alarm/PendingIntent 如何将 registerNetworkCallback 与 PendingIntent 一起使用? - How to use registerNetworkCallback with PendingIntent? 根据此代码,如果返回的Future如何取消FutureTask <V> 被限制在实例中? - According to this code, how could a FutureTask be cancelled if the returned Future<V> is confined in instance? 如何识别被取消的 ScheduledFuture 是否真的没有被取消? - How to identify if cancelled ScheduledFuture is actually not cancelled? 如何在指定时间发送PendingIntent - How to send PendingIntent at Specified time 如何在特定时间取消PendingIntent? - How to cancel PendingIntent at specific time? 使用 OpenId AppAuth-Android 库时,带有隐式意图的 PendingIntent 返回取消的异常 - PendingIntent with implicit intent returning cancelled exception when using OpenId AppAuth-Android library 当闹钟时间与实时时间相距1小时的倍数时,会触发PendingIntent - PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM