Android使用AIDL(Android接口定义语言)来进行应用之间通信。
首先需要新建一个AIDL文件,在App中(需要注意的是添加之后需要先Rebuild一下才能开始使用这个类)。
之后再AppService中的onBinder方法中使用这个AIDL:
public class AppService extends Service {
public AppService() {
}

@Overridepublic IBinder onBind(Intent intent) {       return new IAppServiceRemoteBindler.Stub() {        @Override        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {        }    };}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {    return super.onStartCommand(intent, flags, startId);}@Overridepublic void onCreate() {    super.onCreate();    System.out.println("Service start");    new Thread(){        @Override        public void run() {            super.run();            running = true;            while (running){                System.out.println(data);                try {                    Thread.sleep(1000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }.start();}@Overridepublic void onDestroy() {    super.onDestroy();    System.out.println("service destroyed");    running = false;}private String data = "默认数据";private boolean running = false;

}
之后需要在AnotherApp中绑定App中的服务:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {

private Intent serviceIntent;private EditText etInput;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    serviceIntent = new Intent();    serviceIntent.setComponent(new ComponentName("com.chenshipeng.startservicefromanotherapp","com.chenshipeng.startservicefromanotherapp.AppService"));    etInput = (EditText)findViewById(R.id.etInput);    findViewById(R.id.btnStartAppService).setOnClickListener(this);    findViewById(R.id.btnStopAppService).setOnClickListener(this);    findViewById(R.id.bindAppService).setOnClickListener(this);    findViewById(R.id.unBindAppService).setOnClickListener(this);    findViewById(R.id.btnSync).setOnClickListener(this);}@Overridepublic void onClick(View v) {    switch (v.getId()){        case R.id.btnStartAppService:            startService(serviceIntent);            break;        case R.id.btnStopAppService:            stopService(serviceIntent);            break;        case R.id.bindAppService:            bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);            break;        case R.id.unBindAppService:            unbindService(this);            bindler = null;            break;           }}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {    System.out.println("bind service");    System.out.println(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}

}

更多相关文章

  1. Android(安卓)Annotations(1)
  2. Android(安卓)GridView 方格中图标与文字如何同时存在
  3. Android中自定义适配器的使用
  4. android mediaplayer概况
  5. Android(安卓)MVP代码生成插件MVPHelper使用教程
  6. android接收adb发送的系统广播及自定义广播
  7. Android(安卓)ApiDemos示例解析(40):App->Service->Local Service
  8. Material Design系列,自定义Behavior实现Android知乎首页
  9. Android(安卓)自定义EditText (限制表情输入&超出长度提示)

随机推荐

  1. android系统服务启动顺序
  2. Android监听Home键和最近任务
  3. Android(安卓)识别SIM卡类型
  4. Android版本和API Level对应关系
  5. android优化工具
  6. 【Android】自动提示匹配之AutoCompleteT
  7. Android学习笔记(11)————Android的私
  8. Android(安卓)view 的cache
  9. android NetWorkHelper 网络工具类
  10. 【Android】网络状态