Android Two service mutual monitoring implementation code _android

Source: Internet
Author: User
Tags stub terminates

Implementation of mutual monitoring between two service

In actual development, you may need to use two service to monitor each other, this example is to implement this function for reference.

Service A:

public class ServiceA extends Service {private static final String TAG = ServiceA.class.getSimpleName ();
  Mybinder Mbinder;
  Myserviceconnection mserviceconnection;

  Pendingintent mpendingintent;

    @Override public void OnCreate () {super.oncreate ();
    if (mbinder==null) {mbinder=new mybinder ();
  } mserviceconnection=new myserviceconnection (); @Override public int Onstartcommand (Intent Intent, int flags, int startid) {ServiceA.this.bindService (new inte
    NT (Servicea.this,serviceb.class), mserviceconnection, context.bind_important);
    Mpendingintent=pendingintent.getservice (this,0,intent,0);
    Notification.builder builder=new Notification.builder (this); Builder.setticker ("daemon A Starts"). Setcontenttext ("I'm here to guard Service B"). Setcontenttitle ("daemon Service A"). Setsmallicon
    (R.mipmap.ic_launcher). Setcontentintent (mpendingintent). Setwhen (System.currenttimemillis ());

    Notification Notification=builder.build ();Startforeground (startid,notification);
  return start_sticky;
  @Override public IBinder onbind (Intent Intent) {return mbinder; public class Mybinder extends Ibridgeinterface.stub {@Override public String getName () throws Remoteexcepti
    On {return "name:" +tag; Class Myserviceconnection implements Serviceconnection {@Override public void onserviceconnected (compon
      Entname componentname, IBinder ibinder) {String name=null;
      try {name= IBridgeInterface.Stub.asInterface (ibinder). GetName ();
      catch (RemoteException e) {e.printstacktrace ();
    } toast.maketext (servicea.this,name+ "Connection succeeded", Toast.length_short). Show (); @Override public void onservicedisconnected (ComponentName componentname) {Toast.maketext (servicea.this,t

      ag+ "Disconnect", Toast.length_short). Show ();

      ServiceA.this.startService (New Intent (Servicea.this,serviceb.class)); ServiceA.this.bindService (New INtent (Servicea.this,serviceb.class), mserviceconnection, context.bind_important); }
  }


}

Service B:

public class ServiceB extends Service {private static final String TAG = ServiceB.class.getSimpleName ();
  Private Pendingintent mpendingintent;
  Private Mybinder Mbinder;

  Private Myserviceconnection mserviceconnection;
  @Override public IBinder onbind (Intent Intent) {return mbinder;
    @Override public void OnCreate () {super.oncreate ();
    if (Mbinder = = null) {Mbinder = new Mybinder ();
  } mserviceconnection = new Myserviceconnection (); @Override public int Onstartcommand (Intent Intent, int flags, int startid) {this.bindservice (new Intent (Servic
    Eb.this, Servicea.class), mserviceconnection, context.bind_important);
    Mpendingintent = Pendingintent.getservice (this, 0, intent, 0);

    Notification.builder Builder = new Notification.builder (this); Builder.setticker ("daemon service B Started"). Setcontenttext ("I'm here to guard service a"). Setcontenttitle ("Guardian service B"). Setsmallicon (R.mipmap.ic_launcher). Setcontentintent (Mpendingintent). Setwhen (System.currenttimemillis ());
    Notification Notification = Builder.build ();

    Startforeground (startid, notification);
  return start_sticky; public class Mybinder extends Ibridgeinterface.stub {@Override public String getName () throws Remoteexcepti
    On {return "name:" +tag; Class Myserviceconnection implements Serviceconnection {@Override public void onserviceconnected (compon
      Entname componentname, IBinder ibinder) {String name=null;
      try {name=ibridgeinterface.stub.asinterface (ibinder). GetName ();
      catch (RemoteException e) {e.printstacktrace ();
    Toast.maketext (serviceb.this, name + "Connection succeeded", Toast.length_short). Show (); @Override public void onservicedisconnected (ComponentName componentname) {Toast.maketext (Serviceb.this,

      TAG + "Disconnect", Toast.length_short). Show ();
    ServiceB.this.startService (New Intent (Serviceb.this, Servicea.class));  ServiceB.this.bindService (New Intent (Serviceb.this, Servicea.class), mserviceconnection, context.bind_important); }
  }


}

Ibridgeinterface.aidl

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

Interface:

public class Mainactivity extends activity {


  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);

    StartService (This, Servicea.class) (new Intent);
    StartService (The new Intent (this, serviceb.class));
  }

Androidmanifest.xml

<service android:name= ". Services. ServiceA "/>
    <service
      android:name=". Services. ServiceB "
       android:process=": Remote "/>

Because it involves a cross process, the onserviceconnected () method uses the

IBridgeInterface.Stub.asInterface (IBinder). GetName ();
Instead of direct type conversions

((Servicea.mybinder) 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 if the service terminates.

The value returned must be one of the following constants:

Start_not_sticky

If the system terminates the service after the Onstartcommand () is returned, the service will not be rebuilt unless there is a pending Intent to be passed.

Start_sticky

If the system terminates the service after the Onstartcommand () is returned, the service is rebuilt and Onstartcommand () is called, but the last Intent is never delivered again. Conversely, unless there is a suspend Intent to start the service (in which case the Intent will be passed), the system will invoke Onstartcommand () via an empty Intent. This applies to media players (or similar services) that do not execute commands but run indefinitely and wait for a job.

Start_redeliver_intent

If the system terminates the service after the Onstartcommand () is returned, the service is rebuilt and Onstartcommand () is invoked by the last Intent passed to the service. Any pending Intent are passed sequentially. This applies to services that proactively perform jobs that should be recovered immediately, such as downloading files.

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.