14. Android Learning Note _android callback function triggers several ways to broadcast a static object

Source: Internet
Author: User

First, by means of broadcasting:

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 void Login (final  Loginonclicklistener listener) {        new Intent (context, Loginactivity. class );        Intent.setflags (intent.flag_activity_new_task);         New Broadcastreceiver () {            publicvoid  onreceive (context context, Intent Intent) {                Listener.handlelogin (true, "receiver: Broadcastreceiver. ");            }        };         New Intentfilter ("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

Second, through the static object:

I encountered a problem in the project, the third party called the SDK, my SDK has a login method, in the login method and launch an SDK inside the login interface, after the user login successful, how to notify the login method is a third party. Later thought of the broadcast, such as the first way is possible.

Later I saw someone else using static objects in their code, and then I tried the following way. Because static objects are immutable, no matter how many objects are created by the class they reside in. Here are the specific implementations:

 Public classSdklibplatform {PrivateActivity context; PrivateSdklibplatform () {}Private StaticSdklibplatform platform =NewSdklibplatform ();  Public Staticsdklibplatform getinstance () {if(Platform = =NULL) {            return NewSdklibplatform (); }        returnplatform; }     Public StaticCallback Listener; /*** Login Method * *@paramListener * Callback function*/     Public voidLogin (Callback Callback) {//Save this callback function on the Listener objectListener =callback; Intent Intent=NewIntent (context, loginactivity.class);        Intent.setflags (Intent.flag_activity_new_task); //Start Activitycontext.startactivity (Intent); } SDKLibPlatform.listener.handle (False, "login .....) ");     Public InterfaceCallback { Public voidHandleBooleanstatus, String message); }}

After Loginactivity is started, the user's login success or failure is the handle method that invokes the listener object, which can be notified to a third party.

if (Code==1) {//Login succeeded    SDKLibPlatform.listener.handle (true, "login success ....) ");} Else {    SDKLibPlatform.listener.handle (false, "login false ....) ");}

Code for third-party calls:

Sdklibplatform.getinstance (). Login (new  Callback () {                                @Override                                publicvoid handle (boolean  status,string message) {                                    System.out.println (status+"-================== ==> "+message);                                  }                            );

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.