7

我注册了我计划在给定时间执行的警报,根据计划列表的大小,它可以是许多警报。但是我有两个问题我还不清楚:

1) 我如何在操作系统中查询我注册的 Pending Intents?我需要这个进行测试。我想要的伪代码是这样的:

List<PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));

2)查看我创建的未决意图,我提供了一个操作和额外的数据(计划 ID)。

private Intent getSchedeuleIntent(Integer id) {

    Intent intent = new Intent(AppConstants.INTENT_ALARM_SCHEDULE);
    intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA, id);

    return intent;
}

但是我们也说intent有FLAG_CANCEL_CURRENT。它会以相同的操作取消所有待处理的意图,还是必须同时执行相同的操作和额外的数据?

PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, getSchedeuleIntent(schedule.id), PendingIntent.FLAG_CANCEL_CURRENT);

我的代码

@Override
public void run() {

    List<ScheduledLocation> schedules = dbManager.getScheduledLocations();
    if(schedules == null || schedules.isEmpty()){
        return;
    }

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    //alarmManager.

    // we need to get the number of milliseconds from current time till next hour:minute the next day.
    for(ScheduledLocation schedule : schedules){

        long triggerAtMillis = DateUtils.millisecondsBetweenNowAndNext(now, schedule.hour, schedule.minute, schedule.day);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, getSchedeuleIntent(schedule.id), PendingIntent.FLAG_CANCEL_CURRENT);

        alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, MILLISECONDS_IN_WEEK, pendingIntent);
    }

    // List<PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));

}


private Intent getSchedeuleIntent(Integer id) {

    Intent intent = new Intent(AppConstants.INTENT_ALARM_SCHEDULE);
    intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA, id);

    return intent;
}
4

1 回答 1

9

1 如何在操作系统中查询我注册的待处理意图?

我不确定你可以,但你可以检查一个特定PendingIntent的是否已注册:

private boolean checkIfPendingIntentIsRegistered() {
    Intent intent = new Intent(context, RingReceiver.class);
    // Build the exact same pending intent you want to check.
    // Everything has to match except extras.
    return (PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE) != null);
}

2 它会取消所有具有相同操作的待处理意图,还是必须同时执行相同操作和额外数据?

它将取消所有PendingIntent被解析为相等的。

究竟是什么意思

android的java文档说:

出于意图解析(过滤)的目的,确定两个意图是否相同。也就是说,如果它们的动作、数据、类型、类和类别相同。这不会比较意图中包含的任何额外数据。

您可以在此处阅读 7391 行:https ://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java

总而言之,PendingIntent除了附加功能之外,所有构建完全相同的东西都将被取消。

于 2012-11-08T15:45:13.640 回答