In my main activity i bind a service.

    //main activity class
    public void startNotificationService(){
    // add notice service
    Context appContext = getApplicationContext();
    Intent intent = new Intent(appContext,NotificationService.class);
    appContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

when the service is created a broadcast receiver is registered, so when ever Android framework send a broadcast the service can catch it and push a notification.

    // service class
    @Override
    public void onCreate() {
    handlerMessage = new UnsolicitedMessageHandler();
    IntentFilter intentFilter = new IntentFilter("disassociation");
    intentFilter.addAction("remediation");
    registerReceiver(new UnsolicitedMessageHandler (), intentFilter);
}

the following is the handle class of the broadcast receiver

class UnsolicitedMessageHandler extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String message;
            if (action.equals("disassociation")) {
                message = intent.getStringExtra("frameName");
                showNotification(R.drawable.disconnect,
                        "Disassocation Notification", message);
            }
            if (action.equals("remediation")) {
                message = intent.getStringExtra("remMsg");
                showNotification(R.drawable.scan, "Remediation Notification",
                        message);
            }
        }
    }

Everything works fine, the problem is that every time when i kill the app and lunches it again, a broadcast is received by UnsolicitedMessageHandler(i know, the system absolutely don't send a broadcast) It's really confusing and frustration. Anyone can help me? Thanks in adnvance.

有帮助吗?

解决方案

I think this is because whenever you kill and start your app again you receive STICKY brodcast.

In onReceive use isInitialStickyBroadcast() to check if current brodcast you are processing is STICKY. If that is STICKY then just ignore and do no processing

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top