[learning notes] AIDL cross-application Service application
AIDL: application programming interface definition language
Note: whenever you create or modify an AIDL file, you need to execute the Build- > Rebuild Project action to take effect.
1. Control the Service of another App through one App
Set a widget with intent in the MainActivity of the App to be called. The parameter is the name of the widget, one is the name of the package to be called, and the other is the name of the service class that is called.
Then use startService and stopService in the MainActivity of the App to be called to control the Service of another program.
Second, bind the Service of another App through one App
On the basis of the above, create a new AIDL file in the called App
Re-onBind the method in the called service class, and the return value implements the method of the interface generated by the newly created AIDL file.
In the called MainActivity, I communicate with the Service I learned before, using bindService and unbindService methods, and of course, I also implement onServiceConnected and onServiceDisconnected methods. You can see my previous blog.
Third, synchronize data to the bound Service
1) write another method setData in the interface created by AIDL and implement it in MyService
2) in the called App, you need to create the same file as the called AIDL file
3) the binder of the class that creates an AIDL file in the MainActivity of the called App
IAppServiceRemoteBinder binder=null
4) associate binder with service in the onServiceConnected method
Using binder.setData method to transfer data to binder to complete communication
Binder.setData (edt.getText () .toString ())