Activity and Service can communicate in three ways: activityservice
What I saw in my blog is pretty good. I borrowed it to share it.
Inherit the Binder class
This method can be used only when your Acitivity and Service are in the same Application and process. For example, you have a background Service that plays background music, in this case, you can use this method for communication.
Use an example to describe how to use it:
1. Let's look at the Service statement:
Public class LocalService extends Service {// instantiate the custom Binder class private final IBinder mBinder = new LocalBinder (); // Random number generator private final Random mGenerator = new Random (); /*** custom Binder class. This is an internal class, so you can know the objects of its peripheral class. Through this class, let the Activity know its Service object */public class LocalBinder extends Binder {LocalService getService () {// return the Service object associated with the Activity, so that in the Activity, you can call some public methods and public properties in the Service to return LocalService. this ;}@ Override public IBinder onBind (Intent intent) {return mBinder;}/** public method, the Activity can call */public int getRandomNumber () {return mGenerator. nextInt (100 );}}
Define an internal class in the Service, a subclass of the Binder. Use this class to pass the Service object to the Activity, so that the Activity can call the public methods and public properties in the Service, however, this method must be in the same process and Application.
2. Check the code of the corresponding Activity again:
Public class BindingActivity extends Activity {LocalService mService; boolean mBound = false; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Override protected void onStart () {super. onStart (); // bind the Service. After binding, the onServiceConnected method Intent intent = new Intent (this, LocalService. class); bindService (intent, mConnection, Context. BIND_AUTO_CREATE) ;}@ Override protected void onStop () {super. onStop (); // unbind the Service, which saves the memory if (mBound) {unbindService (mConnection); mBound = false;}/** the user clicks the button, read the random number */public void onButtonClick (View v) {if (mBound) {// use the Service object to read the random number int num = mService. getRandomNumber (); Toast. makeText (this, "number:" + num, Toast. LENGTH_SHORT ). show () ;}/ ** fixed ServiceConnection, used to bind */private ServiceConnection mConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName className, IBinder Service) of the service) {// the LocalService has been bound and is strongly converted to an IBinder object. Call the method to obtain the LocalService object LocalBinder binder = (LocalBinder) service; mService = binder. getService (); mBound = true ;}@ Override public void onServiceDisconnected (ComponentName arg0) {mBound = false ;}};}
Here we get the LocalService object through IBinder, and then call its Public method.
Use Messenger
The preceding method can only be used in the same process. If you want to communicate with the Service of another process, you can use Messenger.
In fact, there is also AIDL to implement IPC, but it is recommended to use Messenger, there are two advantages:
1. using Messenger is much simpler than using AIDL.
2. When using Messenger, all messages sent from the Activity will be placed in a queue and will not request services at the same time, so it is thread-safe. If your program is to access the Service through multiple threads, you can use AIDL. Otherwise, it is best to use the Messenger method.
However, the underlying implementation of Messenger is AIDL. Let's take a look at the implementation method and first look at the Service code:
Public class MessengerService extends Service {/** is used for the Message Type in Handler */static final int MSG_SAY_HELLO = 1; /*** Handler */class IncomingHandler extends Handler {@ Override public void handleMessage (Message msg) {switch (msg. what) {case MSG_SAY_HELLO: Toast. makeText (getApplicationContext (), "hello! ", Toast. LENGTH_SHORT ). show (); break; default: super. handleMessage (msg) ;}}/ *** this Messenger can be associated with the Handler in the Service. The Activity uses this object to send a Message to the Service, and the Service uses Handler for processing. */Final Messenger mMessenger = new Messenger (new IncomingHandler ();/*** when the Activity is bound to a Service, an IBinder is returned using this method, activity uses the Messenger created by the IBinder to communicate with the Handler of the Service */@ Override public IBinder onBind (Intent intent) {Toast. makeText (getApplicationContext (), "binding", Toast. LENGTH_SHORT ). show (); return mMessenger. getBinder ();}}
Activity Code Implementation
Public class ActivityMessenger extends Activity {/** the Messenger object that sends a Message to the Service */Messenger mService = null;/** determines whether a Service is bound */boolean mBound; private ServiceConnection mConnection = new ServiceConnection () {public void onServiceConnected (ComponentName className, IBinder service) {// the Activity has been bound to a Service // create a Messenger object through the service parameter, this object can send a Message to the Service and communicate with the Service. mService = new Mes Invoke (service); mBound = true;} public void onServiceDisconnected (ComponentName className) {mService = null; mBound = false ;}; public void sayHello (View v) {if (! MBound) return; // send a Message msg = Message to the Service. obtain (null, MessengerService. MSG_SAY_HELLO, 0, 0); try {mService. send (msg);} catch (RemoteException e) {e. printStackTrace () ;}@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main) ;}@ Override protected void onStart () {super. onStart (); // bind Service bindService (new Intent (this, MessengerService. class), mConnection, Context. BIND_AUTO_CREATE) ;}@ Override protected void onStop () {super. onStop (); // unbind if (mBound) {unbindService (mConnection); mBound = false ;}}}
Note: The code written above can only send messages from Activity to Service. If you want to send messages from Service to Activity, you only need to write the code in turn.
Use AIDL
This method is omitted. If you know the above two methods, this method is rarely used.
How to pass a value between an Activity and a service
We recommend that you check the communication methods between Activity and Service.
Communication between activity and service in android Development
You can refer to the music player for more information.
The service updates the progress bar in the activity every time it changes.
BroadcastReceiver should be used for intermediate Communication
Study the multimedia player.