简体   繁体   English

动态注册的 BroadcastReceiver 实例没有触发?

[英]Dynamically registered BroadcastReceiver instance not firing?

I've been trying to find the problem here, but I can't seem to... my onReceive doesn't seem to get called, here's what I have:我一直试图在这里找到问题,但我似乎无法......我的 onReceive 似乎没有被调用,这是我所拥有的:

public abstract class NoUpdatesTimer extends BroadcastReceiver {
    private Context context;
    private PendingIntent pendingIntent;

    public NoUpdatesTimer(Context context) {
        this.context = context;
        Intent intent = new Intent(Constants.ALARM_NO_LOC_UPDATES);
        pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        context.registerReceiver(this, new IntentFilter(Constants.ALARM_NO_LOC_UPDATES));
    }

    public void scheduleCheck(long delayMillis) {
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayMillis, pendingIntent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ...
    }
}

With some debugging, I verified that scheduleChecking is called but the onReceive method is not called.通过一些调试,我验证了 scheduleChecking 被调用但 onReceive 方法未被调用。 I also tried to trigger this code from a shell, using:我还尝试从 shell 触发此代码,使用:

adb shell am broadcast -a rsg.ms.7

(where Constants.ALARM_NO_LOC_UPDATES is "rsg.ms.7"). (其中 Constants.ALARM_NO_LOC_UPDATES 是“rsg.ms.7”)。

Can you tell me what to change so onReceive gets called?你能告诉我改变什么以便 onReceive 被调用吗?

Using the application context instead of the passed in service context seems to make it work, not sure why:使用应用程序上下文而不是传入的服务上下文似乎可以使它工作,不知道为什么:

public abstract class NoUpdatesTimer extends BroadcastReceiver {
    private Context context;
    private PendingIntent pendingIntent;

    public NoUpdatesTimer(Context context) {
        this.context = context.getApplicationContext();
        Intent intent = new Intent(Constants.ALARM_NO_LOC_UPDATES);
        pendingIntent = PendingIntent.getBroadcast(this.context, 0, intent, 0);
        this.context.registerReceiver(this, new IntentFilter(Constants.ALARM_NO_LOC_UPDATES));
    }

    public void scheduleCheck(long delayMillis) {
        AlarmManager alarmManager = (AlarmManager)this.context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayMillis, pendingIntent);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ...
    }
}

The nature of PendingIntent is bound relatively to ApplicationContext . PendingIntent的本质是相对于ApplicationContext for example its usage with Widgets where the broadcast is outside the application.例如,它与广播在应用程序之外的小部件一起使用。 so when you're getting broadcast from PendingIntent in PendingIntent.getBroadcast you have to provide the application context not the lower layers.因此,当您从PendingIntent中的PendingIntent.getBroadcast获得广播时,您必须提供应用程序上下文而不是较低层。 I had the same issue once and it took me a whole day to figure this out.我曾经遇到过同样的问题,我花了一整天的时间才弄明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM