所以,我有这个问题。 我正在使用某种GCM通知解析器的依赖项目。写得有些糟糕,但由于工作原因,我不得不使用它。反正:
使用WakefulBroadcastReceiver启动主服务(扩展IntentService)。 在收到来自GCM的消息后,我做了一些魔术并使用广播将其发送到主应用程序。 在主应用程序中,我不断地使用另一个广播接收器来运行服务,该接收器捕获消息并将所有内容保存在数据库等中。
为什么这么复杂?首先 - 最初它是别人的项目,现在我正在尝试修复错误。其次 - 我无法从依赖到主应用程序项目访问,因此我传递带有广播的消息。
现在,有趣的部分。我需要过滤是否要显示通知。在向我的主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
}
}
和我的“有缺陷的”接收者: 公共类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,但我们可以看到 - 没有成功。
EDIT2: 奇怪的。 我相信,问题在于onStop()方法。可能它叫得太早(?)所以我的接收器没有机会工作。当我启动app而没有unRegister一切正常。我当然得到了上面的错误,但仍然......这是一些东西。 有什么想法吗?
答案 0 :(得分:2)
好。问题出在IntentService的想法中。
intentService在onHandleIntent()方法之后自行杀死。 所以这个问题的解决方案是将IntentService更改为Service,记住处理停止此事。