Android Best Practice on Updating the UI from BroadcastReceiver to a certain activity -


When I have a broadcast receiver, then android.intent.action.MEDIA_BUTTON is called and I am present without creating a new one Want to update the activity UI, is there any good practice on this?

What I know (can not be true)

1) I can keep the Broadcast receiver in the same class as activity and call. After some activity updateUI function < / P>

2) Create a content subversion?

3) Communicate for the services created by the activity, use ADL (I do not know how to get an existing service if it is registered with any activity)

4) Create a custom filter on the broadcast receiver located in the same class as the activity, and use context.sendBroadcast Custom Filter) and the UI in the custom filter call update (similar to one common?)

The last flow is that The receiver will come from the receiver and will update the UI without any new activity (unless the activity is dead?)

Please link / source code on how to handle this kind of problem give. Thank you very much in advance :)

The easiest way to provide this functionality is to send the broadcast receiver Activity and bound / increase the MyActivity activity of the Register Receiver and unregisterreceiver :

  {Private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver () {@ Override Public Records on Receive (Reference Reference, Intent of Intent) {MyActivity.this.receivedBroadcast}; }}; Override public null at @ iterative () {super.onResume (); IntentFilter iff = new IntentFilter (); Iff.addAction ("android.intent.action.MEDIA_BUTTON"); // Put whatever you want to get in the form of this action. Registry receiver (this. MBORcast receiver, iff); } @ Override Public Wide On Pause () (Super. This.unregisterReceiver);} Get Private Zero Rave (Intent I) {// Get Your Handling Code Here}}  

Depending on the intention you want to receive, you have your AndroidManifest.xml file.


Comments