跨应用Service
2015-09-07 18:48
483 查看
一、跨应用启动Service:
5.0版本后只支持显式Intent,可用
i.setComponent(new Component("包名",“包名.服务类名”))
来实现跨应用启动Service.
二、跨应用绑定Service:
需要在被绑定Service的App中建立AIDS文件,并且在onBind中返回,如下
然后就可以调用方法实现绑定了
三、跨应用通信
步骤1:在app1中新建AIDL文件
步骤2:在app2中new—>Folder—>AIDL Folder建立,在里面新建一个包,包名为app1的包名,再将app1的AIDL文件复制过来。 就是通过AIDL中的setData方法来实现通信。
步骤3:在app1的MyService中的onBind里实现
步骤4:在app2中的onServiceConnected方法实现binder与app1中的MyService的连接
步骤5:现在可用binder.setData()方法来传递数据给MyService了
5.0版本后只支持显式Intent,可用
i.setComponent(new Component("包名",“包名.服务类名”))
来实现跨应用启动Service.
二、跨应用绑定Service:
需要在被绑定Service的App中建立AIDS文件,并且在onBind中返回,如下
public IBinder onBind(Intent intent) { return new IMyService.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } }; }
然后就可以调用方法实现绑定了
case R.id.button3: bindService(i,this,Context.BIND_AUTO_CREATE); break; case R.id.button4: unbindService(this); break;
三、跨应用通信
步骤1:在app1中新建AIDL文件
// IMyService.aidl package com.example.wjb.helloworld; // Declare any non-default types here with import statements interface IMyService { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); void setData(String data); }
步骤2:在app2中new—>Folder—>AIDL Folder建立,在里面新建一个包,包名为app1的包名,再将app1的AIDL文件复制过来。 就是通过AIDL中的setData方法来实现通信。
步骤3:在app1的MyService中的onBind里实现
public IBinder onBind(Intent intent) { return new IMyService.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public void setData(String data) throws RemoteException { MyService.this.data=data; } }; }
步骤4:在app2中的onServiceConnected方法实现binder与app1中的MyService的连接
private IMyService binder = null; @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = IMyService.Stub.asInterface(service); }
步骤5:现在可用binder.setData()方法来传递数据给MyService了
if(binder!=null){ try { binder.setData(et.getText().toString()); }catch (RemoteException e){ e.printStackTrace(); } }
相关文章推荐
- 读书不二
- python——ADSL拨号程序
- contiki netstack数据收发结构
- 程序崩溃80%的原因还是因为对象没有实例化就使用
- Android学习笔记(六)Fragment的生命周期
- 所有节点之间的最短路问题
- 清空memcache缓存的一些资料
- android之屏幕适配之二实践切图适配不同的平板
- vim分屏操作
- redis linux安装 [adlist.o] Error jemalloc/jemalloc.h: No such file or directory
- mybatis多表查询,自动生成id
- Java基础针对自己薄弱环节总结10(线程下)
- hdu4747 Mex (线段树 好题)
- 结构体指针引出的问题
- react-native试玩(10)-导航栏
- java 类的总结
- Journey源码分析三:模板编译
- JDK各个版本的新特性jdk1.5-jdk8
- 使用Junit等工具进行单元测试
- 在ContextLoaderListener中使用注解注入的类和job中使用注解注入的类