当两个应用程序在其清单中定义相同的BroadcastReceiver类时会发生什么?

时间:2015-09-21 04:53:32

标签: android broadcastreceiver android-pendingintent

我有一个带有BroadCastReceiver类的Android库项目和一个调度程序类,它定义了为BroadcastReceiver设置重复PendingIntent的方法

//delay for AlarmManager setRepeating method in seconds
//id as requestCode for PendingIntent
public static void scheduleBroadcast(Context context,int delay,int id){
    Intent intent = new Intent(context, SuperReceiver.class);
    intent.putExtra(KEY_DELAY, (long)(delay * 1000));
    intent.putExtra("APP_NAME", context.getPackageName());
    long timeGap = (long)(delay * 1000);

    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + timeGap, timeGap,
            alarmIntent);
}

接收者

@Override
public void onReceive(Context context, Intent intent) {
    Log.e("Chirag-Library","SuperReceiver Received!");
    long delay = intent.getLongExtra(BScheduler.KEY_DELAY, -1);
    Log.e("Chirag-Library","Delay is "+delay+" ms");
    Log.e("Chirag-Library", "By App called  "+ intent.getStringExtra("APP_NAME"));
    Log.e("Chirag-Library",context.getPackageName());
}   

我正在使用这个项目作为我的两个应用程序中的依赖项,这些应用程序以完全相同的方式在其清单中定义接收器

        <receiver android:name="com.chirag.library.SuperReceiver"/>

我从两个具有不同延迟值的应用程序中调用scheduleBroadcast()。

在com.chirag.appone

BScheduler.scheduleBroadcast(getApplicationContext(), 10,111);

在com.chirag.apptwo

BScheduler.scheduleBroadcast(getApplicationContext(), 60,999);

当任何一个PendingIntent被触发时,是否为这两个应用调用了OnReceive()?

是因为PendingIntents具有相同的接收器意图吗?

Logcat显示在OnReceived中同时触发的日志秒

09-21 10:04:19.746  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:04:19.746  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:04:19.746  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:04:19.746  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:05:06.987  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:05:06.987  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:05:06.987  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:05:06.987  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:06:04.427  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:06:04.427  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:06:04.427  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:06:04.427  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:06:40.666  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:06:40.666  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:06:40.666  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:06:40.666  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:06:40.682  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:06:40.682  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:06:40.682  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:06:40.682  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:07:40.766  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:07:40.766  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:07:40.766  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:07:40.766  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:07:40.774  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:07:40.774  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:07:40.775  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:07:40.775  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:08:19.745  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:08:19.745  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:08:19.745  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:08:19.745  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:08:19.754  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:08:19.754  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:08:19.754  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:08:19.754  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:09:19.742  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:09:19.742  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:09:19.742  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:09:19.742  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:09:19.751  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:09:19.751  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:09:19.751  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:09:19.751  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:10:19.843  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:10:19.843  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:10:19.843  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:10:19.843  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:10:19.853  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:10:19.853  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:10:19.853  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:10:19.853  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:11:19.971  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:11:19.971  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:11:19.971  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:11:19.971  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:11:19.979  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:11:19.979  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:11:19.979  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:11:19.980  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:12:20.065  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:12:20.065  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:12:20.066  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:12:20.068  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:12:20.075  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:12:20.075  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:12:20.076  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:12:20.076  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone
09-21 10:13:20.165  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:13:20.165  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ Delay is 60000 ms
09-21 10:13:20.165  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ By App called  com.chirag.apptwo
09-21 10:13:20.165  14673-14673/com.chirag.apptwo E/Chirag-Library﹕ com.chirag.apptwo
09-21 10:13:20.171  14306-14306/com.chirag.appone E/Chirag-Library﹕ SuperReceiver Received!
09-21 10:13:20.171  14306-14306/com.chirag.appone E/Chirag-Library﹕ Delay is 10000 ms
09-21 10:13:20.171  14306-14306/com.chirag.appone E/Chirag-Library﹕ By App called  com.chirag.appone
09-21 10:13:20.171  14306-14306/com.chirag.appone E/Chirag-Library﹕ com.chirag.appone

对我来说更令人困惑的是,为什么距离appone的10s延迟只能在60秒延迟到apptwo之后被触发一次?

这到底发生了什么?

1 个答案:

答案 0 :(得分:0)

  

当任何一个应用程序中的两个应用程序都调用了OnReceive()   PendingIntent被解雇了吗?

没有。 原因如下: 导入PendingIntent的Intent是显式Intent,因此使OnReceive应用程序上下文特定,因此两个应用程序都有自己的接收器,即使名称相同。

  

对我来说更令人困惑的是为什么因为appone的10s延迟被解雇了   从apptwo延迟60s只有一次?

在对setRepeating()进行一些错误研究后,我发现了这个https://code.google.com/p/android/issues/detail?id=161244

在API&gt; = 21上说setRepeating()间隔值重置为60秒,如果小于60秒(这没有记录),这恰好是我的情况。

因此,他们第一次似乎以不同的时间间隔(当前时间+ 10秒和当前时间+ 60秒)发射,然后两者都以60秒的间隔触发。而且,setRepeating()并不总是产生秒精确事件,所以两者都是我的onReceive似乎同时被解雇导致了混乱