本地服务code:`
package app.project.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import app.project.IMyAidlInterface;

public class LocalService extends Service {
MyBinder binder;
MyConn conn;

@Nullable@Overridepublic IBinder onBind(Intent intent) {    return binder;}@Overridepublic void onCreate() {    super.onCreate();    binder = new MyBinder();    conn = new MyConn();}class MyBinder extends IMyAidlInterface.Stub {    @Override    public String getServiceName() throws RemoteException {        return LocalService.class.getSimpleName();    }}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    Toast.makeText(LocalService.this, " 本地服务started", Toast.LENGTH_SHORT).show();    this.bindService(new Intent(LocalService.this, RomoteService.class), conn, Context.BIND_IMPORTANT);    return START_STICKY;}class MyConn implements ServiceConnection {    @Override    public void onServiceConnected(ComponentName name, IBinder service) {    }    @Override    public void onServiceDisconnected(ComponentName name) {        Toast.makeText(LocalService.this, "远程服务killed", Toast.LENGTH_SHORT).show();        //开启远程服务        LocalService.this.startService(new Intent(LocalService.this, RomoteService.class));        //绑定远程服务        LocalService.this.bindService(new Intent(LocalService.this, RomoteService.class), conn, Context.BIND_IMPORTANT);    }}@Overridepublic void onDestroy() {    super.onDestroy();    //开启远程服务    LocalService.this.startService(new Intent(LocalService.this, RomoteService.class));    //绑定远程服务    LocalService.this.bindService(new Intent(LocalService.this, RomoteService.class), conn, Context.BIND_IMPORTANT);}

}

远程服务code:

package app.project.service;import android.app.Service;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;import android.util.Log;import android.widget.Toast;import app.project.IMyAidlInterface;public class RomoteService extends Service {    MyConn conn;    MyBinder binder;    @Nullable    @Override    public IBinder onBind(Intent intent) {        return binder;    }    @Override    public void onCreate() {        super.onCreate();        conn = new MyConn();        binder = new MyBinder();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Toast.makeText(this, " 远程服务started", Toast.LENGTH_SHORT).show();        this.bindService(new Intent(this, LocalService.class), conn, Context.BIND_IMPORTANT);        return START_STICKY;    }    class MyBinder extends IMyAidlInterface.Stub {        @Override        public String getServiceName() throws RemoteException {            return RomoteService.class.getSimpleName();        }    }    class MyConn implements ServiceConnection {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {        }        @Override        public void onServiceDisconnected(ComponentName name) {            Toast.makeText(RomoteService.this, "本地服务killed", Toast.LENGTH_SHORT).show();           //最关键的还是这两句话            //开启本地服务            RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));            //绑定本地服务            RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);        }    }    @Override    public void onDestroy() {        super.onDestroy();        //开启本地服务        RomoteService.this.startService(new Intent(RomoteService.this, LocalService.class));        //绑定本地服务        RomoteService.this.bindService(new Intent(RomoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);    }}

aidl文件:
package app.project;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
String getServiceName();
}
配置文件

<service            android:name=".service.LocalService"            android:enabled="true"            android:exported="true" />        <service            android:name=".service.RomoteService"            android:process=":romoteservice"            android:enabled="true"            android:exported="true">service>

更多相关文章

  1. Unity—Android通讯
  2. Android(安卓)面试经验 - Android(安卓)进程间的通信
  3. Android(安卓)启动过程详解
  4. C#开发Android手机应用全接触(mono for android)
  5. Android特效第四篇:Android抽屉实现
  6. Android启动过程深入解析
  7. Android官方架构组件DataBinding双向绑定篇: 观察者模式的殊途同
  8. [置顶] 【Android】 基于XMPP Smack框架 开发QQ教程:目录
  9. Android(安卓)系统完整的权限列表

随机推荐

  1. A first hand look at building an Andro
  2. Android(安卓)四种Http协议详解
  3. android启动优化
  4. Android(安卓)菜单资源
  5. Android之VideoView窗口/全屏播放
  6. Android—文字轮播
  7. Android(安卓)4.4 上实现透明导航栏和状
  8. Android(安卓)Progressbar进度条显示
  9. android软键盘问题
  10. Android(安卓)sqlite例子 有外键的使用