Android in Localbroadcastmanager use

Source: Internet
Author: User

Today there is a problem of communication between acitivity, because we are wrapping the activity with tabactivity (what we call a), so the two activity is at the same time. When you start other activities in tabactivity to handle some business, and when the business process is complete, the results are returned through Onactivityresult (), we need to update the a based on the returned results, but now the problem is, how do we update a?

I found that Tabactivity's Onactivityresult () was executed before the onresume of a, that is, I can do some processing in tabactivity, update the state when a executes onresume, The first thing I think about is that through sharedpreference, by writing an identity, let a to read the identity. But this is obviously not satisfying, and it's not a good idea to write a file to pass the state.

Alternatively, we can transmit the status via broadcast, but the broadcast is too heavy, and as an app, this system-level operation should not be done, and system-level broadcasts can also bring security issues. Once captured by other applications, it can be exploited.

Finally, we learned from colleagues that there is something that can help us solve this problem: Localbroadcastmanager.

It comes from Android's support package, The class name is Android.support.v4.content.LocalBroadcastManager, which is used to send broadcast between different components within the same application, just in accordance with our scene. At the same time, it sends the broadcast only in the app spreads, does not leak to other applications, the security also has the guarantee. And it's very simple to use, similar to a normal broadcast.

Use the following method, which is called by the receiver, which is our A.

?       ? Broadcastreceiver camerapicbroadcastreceiver = new Broadcastreceiver () {

@Override

public void OnReceive (Contextcontext, Intent Intent) {

Boolean issuccess = Intent.getextras (). Getboolean (mconstants.key_login_sucess);

?    ?    ?    ?    ? ?.......//Update status

}

};

intentfilter intentfilter = new Intentfilter ( mconstants.key_intent_login_result);

final Localbroadcastmanager Localbroadcastmanager = Localbroadcastmanager. getinstance (CTX);

Localbroadcastmanager.registerreceiver (camerapicbroadcastreceiver, intentfilter);

The sender of the broadcast has the following wording:

?    ?    ?    ? ? Intent loginsucessintent = new Intent ();

Loginsucessintent.setaction (Mconstants.key_intent_login_result);

Loginsucessintent.putextra (mconstants.key_login_sucess, false);

Localbroadcastmanager.getinstance (context). Sendbroadcast (loginsucessintent);

Localbroadcastmanager is a singleton, and it can be used in a simple way, and it solves the interaction problem between service and activity.

Android in Localbroadcastmanager use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.