android - 获取 Android 操作系统中已注册的待定 Intent 列表

标签 android

我注册了计划在给定时间执行的警报,根据计划列表的大小,可能会有很多警报。但是我有两个问题对我来说仍然不清楚:

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

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

2) 查看我创建的待定 Intent ,我提供了一个操作和额外数据(计划 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。它会用相同的操作取消所有未决的 Intent ,还是它必须同时使用相同的操作和额外的数据?

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

最佳答案

1 如何向操作系统查询我注册的 Pending Intent?

我不确定你是否可以,但你可以像这样检查特定的 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 它会取消所有具有相同操作的未决 Intent ,还是它必须同时具有相同的操作和额外的数据?

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

等于到底是什么意思?

android的java文档说:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

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

总而言之,所有构建完全相同的 PendingIntent 除了 extras 将被取消。

关于android - 获取 Android 操作系统中已注册的待定 Intent 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292085/

相关文章:

android - 设置背景图像警报对话框

android - 我应该忽略用户的这个异常吗

Android: "nowrap"在 TextView 的 Spannable 中

android - 我的 .xml 布局文件的图形表示为空白

java - 如何手动查找android文件的文件路径?

java - 在Java中合并两个类?

android - Android 中的完成处理程序

java - 我怎样才能通过半径与另一个坐标点和距离以公里为单位的坐标点?

java - HttpGet 发送请求时编码错误

android - 关闭一个 ViewController 并在 iOS 中启动另一个(不重复,其他答案不起作用)