Cross-process callback in Android

Source: Internet
Author: User

[Java] During Android Application Development, cross-process callback may occur. For example, if a service is called but the service is asynchronous, a notification must be sent to the customer after the service is completed, in this case, cross-process callback is required. In essence, the Binder mechanism is used for cross-process callback. The process is as follows: 1. define aidlITest. aidl [plain] package com. example. chirpdemo; import com. example. chirpdemo. ITestListener; interface ITest {int getValue (); void setValue (int value); void listen (ITestListener listener);} ITestListener. aidl [plain] www.2cto. compackage com. example. chirpdemo; interface ITestListener {void onFinished (int result);} 2. the Service is defined as follows: [java] package com. example. chirpdem O; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. OS. remoteException; import android. util. log; public class MyService extends Service {final private static String TAG = "MyService"; public class ServiceImpl extends ITest. stub {private int mValue; private ITestListener mListener; public ServiceImpl () {mValue = 0 ;}@ Override public int getVal Ue () throws RemoteException {return mValue;} @ Override public void setValue (int value) throws RemoteException {mValue = value; if (null! = MListener) {mListener. onFinished (-1) ;}}@ Override public void listen (ITestListener listener) throws RemoteException {mListener = listener ;}@ Override public void onCreate () {Log. d (TAG, "onCreate"); super. onCreate () ;}@ Override public void onDestroy () {Log. d (TAG, "onDestroy"); super. onDestroy () ;}@ Override public void onStart (Intent intent, int startId) {Log. d (TAG, "onStart"); super. on Start (intent, startId) ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {Log. d (TAG, "onStartCommand"); return super. onStartCommand (intent, flags, startId) ;}@ Override public boolean onUnbind (Intent intent) {Log. d (TAG, "onUnbind"); return super. onUnbind (intent) ;}@ Override public void onRebind (Intent intent) {Log. d (TAG, "onRebind"); super. onRebind (intent) ;}@ Overr Ide public IBinder onBind (Intent arg0) {return new ServiceImpl () ;}} 3. the Client is defined as follows: [java] package com. example. easytabdemo; import android. app. activity; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. serviceConnection; import android. graphics. pixelFormat; import android. graphics. drawable. drawable; import android. OS. bundle; Import android. OS. IBinder; import android. OS. remoteException; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. editText; import android. widget. textView; import com. example. chirpdemo. ITest; import com. example. chirpdemo. ITestListener; import com. example. easytabdemo. sshorteta BHost. tabSpec; public class MainActivity extends Activity {final private static String TAG = "MainActivity"; final Intent myIntent = new Intent ("com. pyk. les. ilongExistService "); private boolean startedService = false; private ITest leservice = null; ServiceConnection myServiceConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName, IBinder service) {les Ervice = ITest. stub. asInterface (service); try {leservice. listen (new ITestListener. stub () {@ Override public void onFinished (int result) throws RemoteException {}});} catch (RemoteException e) {// TODO Auto-generated catch block e. printStackTrace () ;}@ Override public void onServiceDisconnected (ComponentName name) {leservice = null; Log. I ("aaaaaaaaaaaa", "onDisconnected:" + Thread. currentTh Read () ;};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); startedService = bindService (new Intent ("com. my. IService "), myServiceConnection, BIND_AUTO_CREATE); Log. I ("aaaaaaaaaaaa", "bindService:" + Thread. currentThread (); Button bt = (Button) this. findViewById (R. id. bt1); bt. setOnClickListener (new OnClickL Istener () {@ Override public void onClick (View arg0) {if (startedService) {if (null! = Leservice) {try {leservice. setValue (leservice. getValue () + 1);} catch (RemoteException e) {e. printStackTrace () ;}}}}); view. setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {if (startedService) {if (null! = Leservice) {try {mEditText. setText (leservice. getValue () + "");} catch (RemoteException e) {e. printStackTrace () ;}}}}) ;}@ Override public void onStart () {super. onStart (); Log. I ("aaaaaaaa", "onStart") ;}@ Override public void onPause () {super. onPause (); Log. I ("aaaaaaaa", "onPause") ;}@ Override public void onResume () {super. onResume (); Log. I ("aaaaaaaa", "onResume");} @ Override public void onStop () {super. onStop (); Log. I ("aaaaaaaa", "onStop") ;}@ Override protected void onDestroy () {if (startedService) {unbindService (myServiceConnection);} Log. I (TAG, "onDestroy"); super. onDestroy ();}}

Related Article

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: 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.