Different broadcastreceiver depending on activity life

140 views Asked by At

Some time ago, I was able to have 2 broadcastreceivers, one declared in my manifest, and the other one, declared in my activity. Like here : What is the best way to handle callback from IntentService

But, since I have changed for LocalBroadcastReceiver, and changed sendOrderedBroadcast with sendBroadcast method, only the one registered in activity is only receiving the broadcast. I have read that localbroadcastreceiver can not be registered in manifest.

So how to wake up a broadcastreceiver which is not registered in an activity ?

1

There are 1 answers

0
Bibu On BEST ANSWER

I found my own answer, so I give the response for those who can be interested in.

I have two BroadcastReceiver class, one used when application is wake up, with :

ActivityBroadcastReceiver myReceiver = new ActivityBroadcastReceiver();

@Override
protected void onResume() {     
    LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, new IntentFilter("com.mybroadcast"));
    super.onResume();
}

and

@Override
protected void onPause() {      
    LocalBroadcastManager.getInstance(this).unregisterReceiver(myReceiver);
    super.onPause();
}

and in my manifest file :

<receiver
        android:name="com.broadcastreceiver.NotificationBroadcastReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="com.mybroadcast" />
        </intent-filter>
    </receiver>

To send my broadcast :

if(!LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast))
            context.sendBroadcast(broadcast);               

I agree that with this, context.sendBroadcast() , everyone can received my broadcast, but in my code, sendBroadcast send only non-sensible data. That is the best solution I got for now.