最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 兩個(gè)Service的相互監(jiān)視實(shí)現(xiàn)代碼

 更新時(shí)間:2016年10月14日 09:19:21   作者:屌絲迷途  
這篇文章主要介紹了Android 兩個(gè)Service的相互監(jiān)視實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

兩個(gè)Service之間相互監(jiān)視的實(shí)現(xiàn)

在實(shí)際開(kāi)發(fā)中可能需要用到兩個(gè)Service相互監(jiān)視的情況,本示例就是實(shí)現(xiàn)此功能以作參考。

服務(wù)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("守護(hù)服務(wù)A啟動(dòng)中")
        .setContentText("我是來(lái)守護(hù)服務(wù)B的")
        .setContentTitle("守護(hù)服務(wù)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+"斷開(kāi)連接",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);

    }
  }


}

服務(wù)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("守護(hù)服務(wù)B啟動(dòng)中")
        .setContentText("我是來(lái)守護(hù)服務(wù)A的")
        .setContentTitle("守護(hù)服務(wù)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 + "斷開(kāi)連接", 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" />

由于涉及到跨進(jìn)程,onServiceConnected() 方法中使用

IBridgeInterface.Stub.asInterface(iBinder).getName();
而不能直接類型轉(zhuǎn)換

((ServiceA.MyBinder)iBinder).getName(); 

onStartCommand

onStartCommand() 方法必須返回整型數(shù)。整型數(shù)是一個(gè)值,用于描述系統(tǒng)應(yīng)該如何在服務(wù)終止的情況下繼續(xù)運(yùn)行服務(wù)。

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

START_NOT_STICKY

  如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則除非有掛起 Intent 要傳遞,否則系統(tǒng)不會(huì)重建服務(wù)。

START_STICKY

  如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則會(huì)重建服務(wù)并調(diào)用 onStartCommand(),但絕對(duì)不會(huì)重新傳遞最后一個(gè) Intent。相反,除非有掛起 Intent 要啟動(dòng)服務(wù)(在這種情況下,將傳遞這些 Intent ),否則系統(tǒng)會(huì)通過(guò)空 Intent 調(diào)用 onStartCommand()。這適用于不執(zhí)行命令、但無(wú)限期運(yùn)行并等待作業(yè)的媒體播放器(或類似服務(wù))。

START_REDELIVER_INTENT

  如果系統(tǒng)在 onStartCommand() 返回后終止服務(wù),則會(huì)重建服務(wù),并通過(guò)傳遞給服務(wù)的最后一個(gè) Intent 調(diào)用 onStartCommand()。任何掛起 Intent 均依次傳遞。這適用于主動(dòng)執(zhí)行應(yīng)該立即恢復(fù)的作業(yè)(例如下載文件)的服務(wù)。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

璧山县| 漳州市| 内丘县| 皋兰县| 容城县| 梁平县| 阿鲁科尔沁旗| 汝南县| 文成县| 洪江市| 武川县| 会昌县| 驻马店市| 综艺| 枣强县| 沙河市| 印江| 庆城县| 台湾省| 宁南县| 南昌县| 广汉市| 梅河口市| 扶余县| 云浮市| 兴城市| 普定县| 顺平县| 汝南县| 镇康县| 调兵山市| 双牌县| 武山县| 嘉定区| 海门市| 县级市| 枞阳县| 长泰县| 沛县| 卓尼县| 乌苏市|