一、项目介绍

【知识准备】

  ①Android Interface definition language(aidl,android接口定义语言),其目的实现跨进程的调用。进程是程序在os中执行的载体,一个程序对应一个进程,不同进程就是指不同程序,aidl实现不同程序之间的调用。

  ②主线程与子线程通信使用handler,handler可以在子线程中发出消息,在主线程处理消息,从而完成线程之间的通信,即使有多个线程,仍然是一个程序。

  ③不同程序之间需要通过aidl通信,通信方式可以有多种,aidl是其中一种。实现的结果就像自己的程序调用自己的其他方法一样,感觉就像一个程序。

  ④业务场景:例如购物app需要支付,购物app是淘宝,支付app是支付宝。所以就需要不同的程序进行通信。

二、首先介绍一个App之间的Service和Activity之间的通信

【项目结构】

  

【MyService】

【提示】

  ①创建Service

  

  ②如果不是通过上述方法创建,一定要记得注册

 <service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>

【代码】

 public class MyService extends Service {
public MyService() {
} @Override
public IBinder onBind(Intent intent) {
return new MyBinder();//return MyBinder通过ServiceConnection在activity中拿到MyBinder
} @Override
public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId);
} public void payService(){
Log.i("MyService", "payService: --------");
} class MyBinder extends Binder{ public void pay(){
payService();
}//通过Binder实例将service中的方法暴露出去
}
}

【layout_main】

添加按钮,点击便于调用

     <Button
android:id="@+id/btn_paly"
android:text="Pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

【MainActivity】

 public class MainActivity extends AppCompatActivity {

     MyService.MyBinder binder = null;
ServiceConnection conn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnPlay = (Button) findViewById(R.id.btn_paly);
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
binder = (MyService.MyBinder) iBinder;
} @Override
public void onServiceDisconnected(ComponentName componentName) { }
}; Intent intent = new Intent(MainActivity.this,MyService.class);
bindService(intent,conn,BIND_AUTO_CREATE);//开启服务 btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (binder!=null){
binder.play();
}
}
});
}
}

【效果】

  

点击后输出service中pay方法中的内容

  

三、两个App之间的Service通信

【项目结构】

  

【步骤】

①在AppPayProvider中创建MyService

  代码同上

【注册】

  Ⅰ、注册时(android:enabled="true"   android:exported="true")设置为true,将Service暴露出去,另一个App才能访问到它

  Ⅱ、添加『<intent-filter>』。由于不是同一个App,通过intent-filter对Intent进行过滤,让另一个app通过action开启服务

         <service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<!--enable:ture设置可用
exported:ture对外暴露 -->
<intent-filter>
<action android:name="com.xqz.apppayprovider.MyService" />
</intent-filter>
</service>

②MainActivity和layout_main保留创建时不作任何修改,但也不要删掉,因为安装程序必须提供起始页面,否则将会出错

③在AppPayProvider中添加AIDL

  

【代码】

  

  

【提示】接口中定义中方法要和Service中的MyBinder中的方法一致

④再创建好AIDL,添加完方法后,android studio需要对这个aidl进行编译,会自动按aidl规范生成一个Binder子类的代码。

  

⑤对MyService中的MyBinder进行修改

  

【提示】继承IPay.Stub。在这之前必须Make Project,否则将没有只能联想

⑥创建AppPayUser对AppPayProvider中的MyService进行操作

【layout-main】

     <Button
android:id="@+id/btnPay"
android:text="pay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

⑦将AppPayProvider中AIDL拷贝到AppPayUser中

【提示】Ⅰ、包名要相同,按目录位置复制,通过下述方法,直接在文件夹进行复制。『此处可以查看项目结构,可以看到包名是相同的』

    Ⅱ、同样拷贝过来后需要Make Project

  

⑧【AppPayUser-MainActivity】

 public class MainActivity extends AppCompatActivity {

     Button btnPay;
private IPay myBinder;//定义AIDL ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { myBinder = IPay.Stub.asInterface(iBinder);
} @Override
public void onServiceDisconnected(ComponentName componentName) { }
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Intent intent = new Intent();
intent.setAction("com.xqz.apppayprovider.MyService");
//表示按照什么进行过滤,启动意图
/*android5.0之后,如果servicer不在同一个App的包中,
需要设置service所在程序的包名
(包名可以到App的清单文件AndroidManifest中查看)*/
intent.setPackage("com.xqz.apppayprovider");
bindService(intent,conn,BIND_AUTO_CREATE);//开启Service btnPay = (Button) findViewById(R.id.btnPay); btnPay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
myBinder.pay();
} catch (RemoteException e) {
//因为是跨程序调用服务,可能会出现远程异常
e.printStackTrace();
}
}
});
}
}

【安装】

  先安装AppPayProvider再安装AppPayUser。

【效果】

  将run中的 视图调到AppPayProvider,点击模拟器AppPayUser中的pay按钮,将会执行AppPayProvider中MyService中pay方法中的内容。

  

四、总结

【跨App和同App之间的区别】

①跨App开启服务是提供服务的App需要设置intent-filter过滤器,控制服务的App需要通过。setAction和setPackage方法进行设置action和包名,才能开启服务。而同App只需要指定启动的service就可。

②跨App的MyBinder实例要通过AIDL获取,两个应用定义同样的接口的方法,通过对应的AIDL名称.Stub.asInterface方法得到binder实例,然后就和同App的myBinder使用么有区别了。

③跨App的MyBinder对象的使用必须捕获异常,而同App不需要。

④可以根据上方简单的例子实现很多类似的功能。

通过AIDL在两个APP之间Service通信的更多相关文章

  1. 两个APP之间怎么调用《IT蓝豹》

    两个app之间怎么调用?   (1):通过显示Intent 启动    首先:配置好B app 的action,即AndroidManifest.xml中声明 <intent-filter> ...

  2. 两个App之间的跳转 并传值

    两个App之间的传值最主要的是方法是 Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.a ...

  3. ios两个app之间跳转,传值的实现

    两个APP之间的跳转是通过[[UIApplication sharedApplication] openURL:url]这种方式来实现的. 1.首先设置第一个APP的url地址 2.接着设置第二个AP ...

  4. Android中两个Activity之间简单通信

    在Android中,一个界面被称为一个activity,在两个界面之间通信,采用的是使用一个中间传话者(即Intent类)的模式,而不是直接通信. 下面演示如何实现两个activity之间的通信. 信 ...

  5. 基于WSAAsyncSelect模型的两台计算机之间的通信

    任务目标 编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递.客户端向服务器端发送"请输出从1到1000内所有 ...

  6. iOS中两个APP之间的跳转和通信

    app间的跳转 一:在第一个app首先要做下面这些操作: 1.在info.plist文件中的Information Property List下添加一项:URL types. 2.点开URL type ...

  7. 跳转APP(app之间互相通信)

    摘要 步骤: 1,添加URL Types项 2,配置URL Scheme 3,其他应用的跳转 4,参数的接收   开发IOS项目的时候,有可能会遇到两个APP应用相互调用的需求,比如说:支付宝支付.. ...

  8. 同一个Tomcat部署两个project之间的通信问题

    同一个tomcat下的两个project是无法通信的. 同一个tomcat中的project能互相调用吗 启动一个tomcat部署多个项目,那么每个项目算是一个线程还是进程呢? Tomcat中的pro ...

  9. iOS - (两个APP之间的跳转)

    一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URL types,再展开Item0,将Item0下的URL ident ...

随机推荐

  1. 文本编辑器(KindEditord)

    1.下载 官网下载:http://kindeditor.net/down.php 本地下载:http://files.cnblogs.com/files/wupeiqi/kindeditor_a5.z ...

  2. c# IPC实现本机进程之间的通信

    IPC可以实现本地进程之间通信.这种用法不是太常见,常见的替代方案是使用wcf,remoting,web service,socket(tcp/pipe/...)等其他分布式部署方案来替代进程之间的通 ...

  3. asp.net core 二 Nginx Supervisor 负载,监听

           ASP.NET Core负载均衡集群搭建(CentOS7+Nginx+Supervisor+Kestrel)          asp.net core在linux运行下,一但命令行退出 ...

  4. js正则表达式入门以及常见用例

    学习正则表达式的最好方法是从例子开始,理解例子之后再自己对例子进行修改,实验.下面给出了不少简单的例子,并对它们作了详细的说明. 假设你在一篇英文小说里查找hi,你可以使用正则表达式hi. 这几乎是最 ...

  5. 减小APK大小

    本篇文章翻译自Reduce APK Size 用户通常不会去下载体积过大的应用程序,特别是当自己的设备连接的是 2G/3G 或者按字节付费的网络.这篇文章描述了如何缩减 APK 的体积大小,以使得更多 ...

  6. SQL 语句 (二) --- SELECT

    1 完整句法: SELECT [ ALL | DISTINCT TOP n [] WITH TIES select_list [INTO [new_table_name] ] [FROM {table ...

  7. TNS-12560: Message 12560 not found; No message file for product=network, facility=TNS报错

    [oracle@localhost bin]$ ./lsnrctl startLSNRCTL for Linux: Version 12.2.0.1.0 - Production on 17-APR- ...

  8. servlet之session设置

    商品对象,购物车对象,servlet的实现 商品: package app02d;public class Product {    private int id;    private String ...

  9. [Luogu 1730]最小密度路径

    Description 给出一张有N个点M条边的加权有向无环图,接下来有Q个询问,每个询问包括2个节点X和Y,要求算出从X到Y的一条路径,使得密度最小(密度的定义为,路径上边的权值和除以边的数量). ...

  10. [ZJOI2006]物流运输 SPFA+DP

    题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...