“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”
Activities and services communicate
Steps for binding local service invocation methods:
- Create an internal binder class inside the service and provide a method that can invoke the service’s methods indirectly
- The onbind method that implements the service returns this internal binder class
- Bind the service to the activity. bindService()
- The onServiceConnected callback method is passed an IBinder object that forces the type to be converted to a custom interface type binder class that calls the interface’s methods.
The following code implements the active binding and starts the service, which invokes its methods through its own internal class, MyBinder.
Activity – > bindService method – “service’s onBind (returning MyBinder self-built inner class instance) -” ServiceConnection. OnServiceConnected () – > by MyBinder instance to invoke the service method,
Any public method in MyBinder can now be invoked in an activity for a specific scenario that tells the service to do what it does.
private void bindService(){ Intent intent = new Intent(TestActivity.this,BindService.class); Log.i(TAG, "bindService()"); bindService(intent, conn, Context.BIND_AUTO_CREATE); Private void unBindService(){log. I (TAG, "unBindService() start...." ); if(flag == true){ Log.i(TAG, "unBindService() flag"); unbindService(conn); flag = false; } } private ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Log.i(TAG, "onServiceDisconnected()"); } @override public void articulation (connected) {// Cut (connected) {// Cut (connected); An IBinder object is passed: Service log. I (TAG, "onServiceConnected()"); /* MyBinder = (MyBinder)service; /* MyBinder = (MyBinder)service; /* Enforce type conversion to custom interface type MyBinder, calling methods in the interface. */ BindService bindService = binder.getService1(); bindService.MyMethod(); flag = true; }}; }Copy the code
public class BindService extends Service { private static final String TAG = "BindService"; private MyBinder myBinder = new MyBinder(); public void MyMethod(){ Log.i(TAG, "BindService-->MyMethod()"); } @Override public IBinder onBind(Intent intent) { Log.i(TAG, "BindService-->onBind()"); return myBinder; } // Create an inner class inside the service MyBinder provides a method getService1(), MyMethod() public class MyBinder extends Binder{public BindService getService1(){return bindService.this; } } @Override public void onCreate() { Log.i(TAG, "BindService-->onCreate()"); super.onCreate(); @override public void onStart(Intent Intent, int startId) {log. I (Intent Intent, int startId) {Log. "BindService-->onStart()"); super.onStart(intent, startId); } @override public int onStartCommand(Intent Intent, int flags, int startId) {Log. "OnStartCommand method called!" ); return super.onStartCommand(intent, flags, startId); } @override public void onDestroy() {log. I (TAG, "BindService-->onDestroy()"); super.onDestroy(); } @Override public boolean onUnbind(Intent intent) { Log.i(TAG, "BindService-->onUnbind()"); return super.onUnbind(intent); }}Copy the code
Since the onStart() method of a Service is called only when startService() starts the Service, be careful when using onStart().
Communicate with the Service and keep it running
What if we want to keep communicating with the Service, but we don’t want the Service to exit with the Activity?
Start startService() and then bindService(). The unbindService() method is executed when no binding is required. This method only triggers the Service’s onUnbind() without destroying the Service. This allows communication with the Service to continue without being destroyed when the Activity is destroyed.
Also note that any one Service is common across the application, that is, a Service can bind not only to MainActivity, but to any other activity, and they all get the same Binder instance once the binding is complete.