Android Learning note _74_ can be broadcast to notify the callback function to trigger

Source: Internet
Author: User

1, such as login. If the following method is outside the call, then how to execute the login operation in Loginactivity, after the successful callback listener interface? If it is a normal class, you can pass the listener class object through the constructor function. However, it is not possible to pass listeners in activity, so consider using broadcast.

 Public voidLoginFinalLoginonclicklistener Listener) {Intent Intent=NewIntent (context, loginactivity.class);        Intent.setflags (Intent.flag_activity_new_task); Broadcastreceiver Mreceiver=NewBroadcastreceiver () { Public voidOnReceive (Context context, Intent Intent) {Listener.handlelogin (true, "receiver. Broadcastreceiver. ");        }        }; Intentfilter Intentfilter=NewIntentfilter ("Android.intent.action.MAIN");        Context.registerreceiver (Mreceiver, Intentfilter); Context.startactivity (intent);}

2. After successful login in the Loginactivity class, send a broadcast notification to log in successfully.

New Intent ("Android.intent.action.MAIN");   Sendbroadcast (intent);

Reference: http://buddie.iteye.com/blog/977352

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.