欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 两个Service的相互监视实现代码

程序员文章站 2024-03-05 11:19:12
两个service之间相互监视的实现 在实际开发中可能需要用到两个service相互监视的情况,本示例就是实现此功能以作参考。 服务a: public cla...

两个service之间相互监视的实现

在实际开发中可能需要用到两个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 intent(servicea.this,serviceb.class),mserviceconnection, context.bind_important);
    mpendingintent=pendingintent.getservice(this,0,intent,0);
    notification.builder builder=new notification.builder(this);
    builder.setticker("守护服务a启动中")
        .setcontenttext("我是来守护服务b的")
        .setcontenttitle("守护服务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 remoteexception {
      return "name:"+tag;
    }
  }

  class myserviceconnection implements serviceconnection {

    @override
    public void onserviceconnected(componentname componentname, ibinder ibinder) {
      string name=null;
      try {
        name= ibridgeinterface.stub.asinterface(ibinder).getname();
      } catch (remoteexception e) {
        e.printstacktrace();
      }


      toast.maketext(servicea.this,name+"连接成功",toast.length_short).show();
    }

    @override
    public void onservicedisconnected(componentname componentname) {
      toast.maketext(servicea.this,tag+"断开连接",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);

    }
  }


}

服务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(serviceb.this, servicea.class), mserviceconnection, context.bind_important);
    mpendingintent = pendingintent.getservice(this, 0, intent, 0);
    notification.builder builder = new notification.builder(this);

    builder.setticker("守护服务b启动中")
        .setcontenttext("我是来守护服务a的")
        .setcontenttitle("守护服务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 remoteexception {
      return "name:"+tag;
    }
  }

  class myserviceconnection implements serviceconnection {

    @override
    public void onserviceconnected(componentname componentname, ibinder ibinder) {
      string name=null;
      try {
        name=ibridgeinterface.stub.asinterface(ibinder).getname();
      } catch (remoteexception e) {
        e.printstacktrace();
      }
      toast.maketext(serviceb.this, name + "连接成功", toast.length_short).show();
    }

    @override
    public void onservicedisconnected(componentname componentname) {
      toast.maketext(serviceb.this, tag + "断开连接", 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 }

界面:

public class mainactivity extends activity {


  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    startservice(new intent(this, servicea.class));
    startservice(new intent(this, serviceb.class));
  }

}

androidmanifest.xml

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

由于涉及到跨进程,onserviceconnected() 方法中使用

ibridgeinterface.stub.asinterface(ibinder).getname();
而不能直接类型转换

((servicea.mybinder)ibinder).getname(); 

onstartcommand

onstartcommand() 方法必须返回整型数。整型数是一个值,用于描述系统应该如何在服务终止的情况下继续运行服务。

返回的值必须是以下常量之一:

start_not_sticky

  如果系统在 onstartcommand() 返回后终止服务,则除非有挂起 intent 要传递,否则系统不会重建服务。

start_sticky

  如果系统在 onstartcommand() 返回后终止服务,则会重建服务并调用 onstartcommand(),但绝对不会重新传递最后一个 intent。相反,除非有挂起 intent 要启动服务(在这种情况下,将传递这些 intent ),否则系统会通过空 intent 调用 onstartcommand()。这适用于不执行命令、但无限期运行并等待作业的媒体播放器(或类似服务)。

start_redeliver_intent

  如果系统在 onstartcommand() 返回后终止服务,则会重建服务,并通过传递给服务的最后一个 intent 调用 onstartcommand()。任何挂起 intent 均依次传递。这适用于主动执行应该立即恢复的作业(例如下载文件)的服务。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!