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); } );