WakefulBroadcastReceiver 中 IntentService 中的 BroadcastReceiver 并不总是有效

BroadcastReceiver in IntentService in WakefulBroadcastReceiver does not always work

所以,我遇到了这个问题。 我正在使用一个依赖项目,它是某种 GCM 通知解析器。写的有点烂,不过因为工作原因,不得不用了。不管怎样:

主要服务(扩展 IntentService)使用 WakefulBroadcastReceiver 启动。 在收到来自 GCM 的消息后,我做了一些魔术并使用广播将其发送到主应用程序。 在主应用程序中,我经常 运行 与另一个 BroadcastReceiver 服务,它捕获消息并将所有内容保存在数据库等中

为什么这么复杂?首先 - 最初它是别人的项目,现在我正在尝试修复错误。其次 - 我无法从依赖项访问主应用程序项目,所以我通过广播传递消息。

现在,有趣的部分。我需要过滤是否要显示通知。在向我的主要 AppService 发送消息时,我会检查它与之前消息的历史记录,然后我决定是否需要向用户显示此消息。但是,无论我的决定是什么,我的依赖项仍然显示我的通知。 所以我添加了另一个广播,在成功验证后我在我的依赖通知构建方法中启动。

代码如下:

我的 WakefulBroadcastReceiver:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(), PushService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}
}

这是我的 Depencency 服务

        public NotificationCheckerReceiver notificationCheckerReceiver;

        ...

        @Override
        protected void onHandleIntent(Intent intent) {
            Bundle extras = intent.getExtras();
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            String messageType = gcm.getMessageType(intent);

            if (!extras.isEmpty()) {

                if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                    //Launch my "approval" receiving broadcast
                    launchBroadcastReceiver(extras, intent);

                    //send broadcast to main app with the message we will parse etc.
                    sendSmsBroadcast(...));


                }
            }

        }


        @Override
        public void onDestroy() {
            unregisterReceiver(notificationCheckerReceiver);
            super.onDestroy();
        }

        //Launch to build notification
        public void showNotification(Bundle extras){
            ...
            //Basic notification builder
        }

        //Receive broadcast from DB if notification was already in the DB
        private void launchBroadcastReceiver(Bundle extras, Intent intent){
            Log.d(TAG, "Broadcast receiver loaded");

            notificationCheckerReceiver = new NotificationCheckerReceiver(new NotiFlag() {
                @Override
                public void onReceiveApproval(Boolean flag, Intent intent, Bundle extras) {
                    Log.d(TAG, "Approved notification show");
                    showNotification(extras);
                    JustPushGcmBroadcastReceiver.completeWakefulIntent(intent);
                }
            }, intent, extras);

            registerReceiver(notificationCheckerReceiver, new IntentFilter(notificationCheckerReceiver.INTENT_EVENT_NAME));
        }

        public void sendSmsBroadcast(String message, boolean isAppOnScreen){
            ...
            //This works
        }

    }

和我的 "faulty" 接收器: public class NotificationCheckerReceiver 扩展 BroadcastReceiver{

    private  final String TAG = getClass().getSimpleName();

    public static final String INTENT_EVENT_NAME = "NOTIFLAG";
    public static final String INTENT_FLAG_KEY = "FLAG";

    Intent intent;
    Bundle extras;

    NotiFlag nofiFlag;

    public NotificationCheckerReceiver(NotiFlag nofiFlag, Intent intent, Bundle extras){
        Log.d(TAG, "Launched constructor NotificationChecker");
        this.nofiFlag = nofiFlag;
        this.intent = intent;
        this.extras = extras;

    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Launched onReceive");
        Boolean bool = intent.getExtras().getBoolean(INTENT_FLAG_KEY);
        Log.d(TAG, "___________Broadcast receiver got something and it is intent: "+bool);

        if (bool != false) {
            nofiFlag.onReceiveApproval(bool, this.intent, this.extras);
        }

    }
}

最后,我从我的主要服务发送的内容:

    public void sendNotificationCheckerBroadcast(Boolean message){
        Intent flag = new Intent(NotificationCheckerReceiver.INTENT_EVENT_NAME);
        flag.putExtra(NotificationCheckerReceiver.INTENT_FLAG_KEY, message);
        DvLogs.d(TAG, "__________Sending intent: "+message);
        sendBroadcast(flag);
    }

我启动 "sendNotificationCheckerBroadcast()" 时发生的一切。我知道我正在发送某种布尔值...就是这样。

有趣的是:它有时会起作用。 我不知道为什么,但是当它出于某种原因启动时 - 一切都很棒。

编辑: 当它工作时,因为有时它会工作,所以我有这个错误:

    01-15 11:20:22.204    3234-3234/pl.digitalvirgo.lafarge E/ActivityThread﹕ Service com.example.name.PushService has leaked IntentReceiver com.example.name.NotificationCheckerReceiver@43042b50 that was originally registered here. Are you missing a call to unregisterReceiver()?
        android.app.IntentReceiverLeaked: Service com.example.name.PushService has leaked IntentReceiver com.example.name.NotificationCheckerReceiver@43042b50 that was originally registered here. Are you missing a call to unregisterReceiver()?
                at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:814)
                at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:610)
                at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1772)
                at android.app.ContextImpl.registerReceiver(ContextImpl.java:1752)
                at android.app.ContextImpl.registerReceiver(ContextImpl.java:1746)
                at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:479)
                at com.example.name.PushService.launchBroadcastReceiver(Unknown Source)
                at com.example.name.PushService.onHandleIntent(Unknown Source)
                at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:157)
                at android.os.HandlerThread.run(HandlerThread.java:61)

也许有什么关系? 我知道我应该在某个地方取消注册这个接收者。尝试了 onStop,但正如我们所见 - 没有成功。

编辑2: 诡异的。 我相信,问题出在 onStop() 方法中。可能它被称为太早(?)所以我的接收器没有机会工作。当我在没有取消注册的情况下启动应用程序时,一切正常。当然,我在上面遇到了错误,但仍然......它是一些东西。 有什么想法吗?

嗯。问题出在 IntentService 的思想中。

intentService 在 onHandleIntent() 方法后自行终止。 所以这个问题的解决办法就是把IntentService改成Service remembering to handle stopping this thing