gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:25:48 24 4
gpt4 key购买 nike

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

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/

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