SoFunction
Updated on 2025-03-11

The mutual monitoring implementation code of two services on Android

Implementation of mutual monitoring between two services

In actual development, two services may need to be monitored by each other. This example is to implement this function for reference.

Service A:

public class ServiceA extends Service {


  private static final String TAG = ();
  MyBinder mBinder;
  MyServiceConnection mServiceConnection;
  PendingIntent mPendingIntent;

  @Override
  public void onCreate() {
    ();

    if(mBinder==null)
    {
      mBinder=new MyBinder();
    }
    mServiceConnection=new MyServiceConnection();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    (new Intent(,),mServiceConnection, Context.BIND_IMPORTANT);
    mPendingIntent=(this,0,intent,0);
     builder=new (this);
    ("Guardian Service A is starting")
        .setContentText("I'm here to protect Service B")
        .setContentTitle("Guardian Service A")
        .setSmallIcon(.ic_launcher)
        .setContentIntent(mPendingIntent)
        .setWhen(());
    Notification notification=();

    startForeground(startId,notification);


    return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }

  public class MyBinder extends  {

    @Override
    public String getName() throws RemoteException {
      return "name:"+TAG;
    }
  }

  class MyServiceConnection implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
      String name=null;
      try {
        name= (iBinder).getName();
      } catch (RemoteException e) {
        ();
      }


      (,name+"Connected successfully",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
      (,TAG+"Disconnect",Toast.LENGTH_SHORT).show();

      (new Intent(,));

      (new Intent(,),mServiceConnection, Context.BIND_IMPORTANT);

    }
  }


}

Service B:

public class ServiceB extends Service {

  private static final String TAG = ();
  private PendingIntent mPendingIntent;
  private MyBinder mBinder;
  private MyServiceConnection mServiceConnection;

  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }

  @Override
  public void onCreate() {
    ();
    if (mBinder == null) {
      mBinder = new MyBinder();
    }

    mServiceConnection = new MyServiceConnection();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    (new Intent(, ), mServiceConnection, Context.BIND_IMPORTANT);
    mPendingIntent = (this, 0, intent, 0);
     builder = new (this);

    ("Guardian Service B is in the process of starting")
        .setContentText("I'm here to protect Service A")
        .setContentTitle("Guardian Service B")
        .setSmallIcon(.ic_launcher)
        .setContentIntent(mPendingIntent)
        .setWhen(());
    Notification notification = ();
    startForeground(startId, notification);

    return START_STICKY;
  }

  public class MyBinder extends  {

    @Override
    public String getName() throws RemoteException {
      return "name:"+TAG;
    }
  }

  class MyServiceConnection implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
      String name=null;
      try {
        name=(iBinder).getName();
      } catch (RemoteException e) {
        ();
      }
      (, name + "Connected successfully", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
      (, TAG + "Disconnect", Toast.LENGTH_SHORT).show();

      (new Intent(, ));
      (new Intent(, ), mServiceConnection, Context.BIND_IMPORTANT);
    }
  }


}

1 interface IBridgeInterface {
2   String getName();
3 }

interface:

public class MainActivity extends Activity {


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);

    startService(new Intent(this, ));
    startService(new Intent(this, ));
  }

}

<service android:name="." />
    <service
      android:name="."
       android:process=":remote" />

Since cross-process involves, the onServiceConnected() method is used

(iBinder).getName();
It cannot be directly converted

(()iBinder).getName(); 

onStartCommand

The onStartCommand() method must return an integer number. An integer is a value that describes how the system should continue to run the service when the service terminates.

The returned value must be one of the following constants:

START_NOT_STICKY

If the system terminates the service after onStartCommand() returns, the system will not rebuild the service unless there is a pending Intent to be passed.

START_STICKY

If the system terminates the service after onStartCommand() returns, the service is rebuilt and onStartCommand() is called, but the last Intent is never re-passed. Instead, unless there is a pending Intent to start the service (in which case these Intents are passed), the system calls onStartCommand() via an empty Intent. This applies to media players (or similar services) that do not execute commands but run indefinitely and wait for jobs.

START_REDELIVER_INTENT

If the system terminates the service after onStartCommand() returns, the service is rebuilt and onStartCommand() is called via the last Intent passed to the service. Any pending Intent is passed in turn. This applies to services that proactively execute jobs that should be restored immediately (such as downloading files).

Thank you for reading, I hope it can help you. Thank you for your support for this site!