获取Android操作系统注册待定意图一览 [英] Get list of registered Pending Intents in Android OS

查看:108
本文介绍了获取Android操作系统注册待定意图一览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

余寄存器,它予安排在给定的时间来执行警报,并且它可以根据预定的列表的大小许多报警。但我有两个问题,这仍然是我不清楚:

1)我如何查询操作系统为待定意图我注册?我需要这个测试。该psudo code什么,我想会是这样的:

 名单,其中,PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));
 

2)查看待处理的意图我创造,我提供了一个动作,额外的数据(附表ID)。

 私人意图getSchedeuleIntent(整数ID){

    意向意图=新的意图(AppConstants.INTENT_ALARM_SCHEDULE);
    intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA,ID);

    返回的意图;
}
 

但我们也说,意图有FLAG_CANCEL_CURRENT。它会取消与同样的动作所有悬而未决的意图,还是有这两个相同的作用和额外的数据?

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

我的code

  @覆盖
公共无效的run(){

    名单< ScheduledLocation>时间表= dbManager.getScheduledLocations();
    如果(时间表== NULL || schedules.isEmpty()){
        返回;
    }

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

    //我们需要得到毫秒的数量从当前时间到明年小时:分钟的第二天。
    对于(ScheduledLocation时间表:时间表){

        长triggerAtMillis = DateUtils.millisecondsBetweenNowAndNext(现在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);
    }

    //名单,其中,PendingIntent> intentsInOS = context.getAllPendingIntentsOfType(AppConstants.INTENT_ALARM_SCHEDULE));

}


私人意图getSchedeuleIntent(整数ID){

    意向意图=新的意图(AppConstants.INTENT_ALARM_SCHEDULE);
    intent.putExtra(AppConstants.INTENT_ALARM_SCHEDULE_EXTRA,ID);

    返回的意图;
}
 

解决方案

1我如何查询操作系统为待定意图我注册?

我不知道你可以,但你可以检查一个特定的 PendingIntent 注册或不喜欢这样的:

 私人布尔checkIfPendingIntentIsRegistered(){
    意向意图=新的意图(背景下,RingReceiver.class);
    //构造要检查完全相同的悬而未决的意图。
    //一切都必须匹配,除了群众演员。
    返程(PendingIntent.getBroadcast(背景下,0,意向,PendingIntent.FLAG_NO_CREATE)!= NULL);
}
 

2是否会取消与同样的动作所有悬而未决的意图,还是有这两个相同的作用和额外的数据?

这将取消所有 PendingIntent 的决心是平等的。

到底是什么等于意味着什么?

的Andr​​oid的Java医生说:

  

确定是否两个目的都是相同的意图的目的   分辨率(过滤)。也就是说,如果它们的作用,数据,类型,类   和类别是相同的。这确实的没有的比较任何额外   包括在意图数据

您可以阅读在这里的7391行:<一href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java" rel="nofollow">https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java

总之,所有的 PendingIntent 正在建立完全一样的,除了群众演员将被取消。

I register alarms which I schedule to execute at given time, and it can be many alarms depending on the size of the scheduled list. But I have two questions which remains unclear to me:

1) How can I query the OS for the Pending Intents I registers? I need this for testing. The psudo code for what I want would be something like this:

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

2) See the pending intent I create, I provide an action and extra data (the schedule id).

private Intent getSchedeuleIntent(Integer id) {

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

    return intent;
}

But we also say that the intent have FLAG_CANCEL_CURRENT. Will it cancel all pending intents with same action, or does it have to both same action AND extra data?

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

My code

@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 How can I query the OS for the Pending Intents I registers?

I'm not sure you can, but you can check if a specific PendingIntent is registered or not like this :

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 Will it cancel all pending intents with same action, or does it have to both same action AND extra data?

It will cancel all the PendingIntent that are resolved to be equal.

What exactly does equal means ?

The java doc of android says:

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.

You can read at the 7391 line here : https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/content/Intent.java

To sum up, all PendingIntent that are build exactly the same except extras will be cancelled.

这篇关于获取Android操作系统注册待定意图一览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆