Communication between Service and Activity (same process)

  • 2021-06-28 09:44:44
  • OfStack

1. When Acitivity and Service are in the same Application and process, they are implemented by inheriting the Binder class.

When an Activity is bound to an Service, it maintains a reference to the Service instance, allowing you to make one method call to the running Service.For example, if you have an Service playing background music in the background, you can communicate in this way.

The code is as follows:


/*************************Service Code ****************************************
/public class LocalService extends Service {
private final IBinder binder = new LocalBinder(); 
public class LocalBinder extends Binder { 
LocalService getService() { 
return LocalService.this; 
} 
} public IBinder onBind(Intent intent) { 
return binder; 
} 
} 
/*****************************Activity Code *************************************/public class BindingActivity extends Activity { 
LocalService localService; 
private ServiceConnection mConnection = new ServiceConnection() { 
public void onServiceConnected(ComponentName className,IBinder localBinder) { localService = (LocalBinder) localBinder.getService(); 
} 
public void onServiceDisconnected(ComponentName arg0) { 
localService = null; 
} 
}; 
protected void onStart() { 
super.onStart(); 
Intent intent = new Intent(this, LocalService.class); 
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
} 
protected void onStop() { 
super.onStop(); 
unbindService(mConnection); 
} 
public void printRandomNumber{ 
int num = localService.getRandomNumber(); 
System.out.println(num); 
}
}

Code explanation:

Starting Service with context.bindService () has the following experience:


context.bindService()->onCreate()->onBind()->Service running
onUnbind() -> onDestroy() ->Service stop

Activity is able to bind thanks to Service's interface onBind().The connection between Service and Activity can be made using ServiceConnection, which requires a new ServiceConnection, rewriting the onServiceConnected and onServiceDisconnected methods.Execute the binding, call the bindService method, and pass in an Intent (explicit or implicit) that has selected the Service to bind and an ServiceConnection instance that you have implemented.1Once the connection is established, you can get an instance of serviceBinder through Service's interface onBind() and an instance reference of Service.1Once an Service object is found, its common methods and properties are available.But in this way, 1 must be in the same process and in the same Application.

How to check if Activity and Service are running in the same process

1 Normally, Activity and Service are in the same package name and do not have the property android:process=":remote" set, both in the same process.

Because a process has only one UI thread, they are in the same thread.

If android:process=":remote" is set, it is cross-process access and belongs to a different process.

Verification method:

Print process information in OnCreate for Activiyt and Service

For example, Log.i ("Tag"), Thread.curentThread (). getId ());


Related articles: