AIDL调用指南
近期有需求要实现两个apk之间的通信,想到用AIDL来实现,现写一个demo学习下AIDL怎样使用。
这里我要实现一个apk(client端)调用还有一个apk(server端)的方法.
先实现server端。代码结构例如以下
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZ3FpYW95ZWJvMjAwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
AIDL文件内容例如以下:
package com.example.testaidl;
interface MyInterface {
void testMethod();
}
MainActivity.java
package com.example.testaidlserver; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
} }
MyService.java
package com.example.testaidlserver; import com.example.testaidl.MyInterface; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log; public class MyService extends Service { @Override
public IBinder onBind(Intent intent) {
Log.i("MyService", "service onBind....");
return new ImplAIDLService();
} private class ImplAIDLService extends MyInterface.Stub { @Override
public void testMethod() throws RemoteException {
Log.i("MyService", "testMode invoked");
}
}
}
Manifest中加入MyService的注冊
<service android:name="com.example.testaidlserver.MyService">
<intent-filter>
<action android:name="com.example.testaidlserver.MyService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
以下是Client端的
aidl文件和server端的要一样
package com.example.testaidl;
interface MyInterface {
void testMethod();
}
MainAcitvity的功能是,绑定server端的service。调用server端的方法
package com.example.testaidlclient; import com.example.testaidl.MyInterface; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {
private boolean mIsBinded = false;
private MyInterface mInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_bind = (Button)findViewById(R.id.btn_bind);
btn_bind.setOnClickListener(this);
Button btn_invoke = (Button)findViewById(R.id.btn_invoke);
btn_invoke.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_bind:
if(mIsBinded) {
unbindService(con);
}else {
bindService(new Intent("com.example.testaidlserver.MyService"), con, Context.BIND_AUTO_CREATE);
}
break;
case R.id.btn_invoke:
try {
mInterface.testMethod();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
} ServiceConnection con = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
mIsBinded = false;
mInterface = null;
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIsBinded = true;
mInterface = MyInterface.Stub.asInterface(service);
}
}; }
执行结果:
AIDL调用指南的更多相关文章
- Android 使用AIDL调用外部服务
好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
1. 概述 bindService() 绑定服务 可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法. 如果调用者activity被销毁了, ...
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
- 腾讯AI开放平台的接口调用指南
最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...
- Android Studio实现Service AIDL
Android Studio实现Service AIDL [日期:2015-01-02] 来源:Linux社区 作者:teenyboy [字体:大 中 小] 今天要开发过程中要用到AID ...
- 大仙说道之Android studio实现Service AIDL
今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...
- Android - AIDL 使用
AIDL(Android Interface Definition Language) 程序员可以利用AIDL自定义编程接口,在客户端和服务端之间实现进程间通信(IPC).在Android平台上,一个 ...
- Binder or AIDL的最简单实践
1.前言: 在Android开发中多进程的合理使用+进程间通信的IPC是一个比较难的点.特别是Android特有的Binder机制,非常复杂,对于应用层开发的初级开发工程师强求深入理解Binder机制 ...
- 让我们一起学习如何使用AIDL,它其实并不难(Android)
前言 该篇文件讲述的是AIDL最基本的使用(创建.调用),关于对于AIDL更深的认识,在后续的随笔中,会持续与大家分享并探讨. 正文 AIDL的定义(什么是AIDL?) AIDL的应用场景(AIDL可 ...
随机推荐
- vim 代码片段:通过vundle插件管理器安装ultisnips |centos6.5|vim7.2
背景:中午醒来,饭都没吃,突然想到要给vim增加个代码片段的功能,因为昨天使用了gedit的代码片段,感觉不错.为什么不直接使用gedit呢?因为我相信把时间投入到vim是不会错的,精通vim就好了. ...
- centos opera 卸载
/usr/local/bin/uninstall-opera
- CodeForces 32C Flea
题目链接:http://codeforces.com/problemset/problem/32/C 本文链接:http://www.cnblogs.com/Ash-ly/p/5513436.html ...
- Web CI过程中的Security解决方案
http://www.infoq.com/cn/articles/WebScan-CI 一. 当前Web应用安全现状 随着中国互联网金融的爆发和繁荣,Web应用在其中扮演的地位也越来越重要,比如Web ...
- 复制对象 copy 与mutable copy
转载 : http://blog.csdn.net/u010962810/article/details/18887841 通过copy方法可以创建可变对象或不可变对象的不可变副本,对于不可 ...
- [NOI2018]屠龙勇士(exCRT)
首先很明显剑的选择是唯一的,直接用multiset即可. 接下来可以发现每条龙都是一个模线性方程.设攻击第i条龙的剑的攻击力为$s_i$,则$s_ix\equiv a_i\ (mod\ p_i)$. ...
- 【R笔记】order函数例子
问题: R中提供的例子不太懂,为什么得出的结果是6 5 2 1 7 4 10 8 3 9呢? (ii <- order(x <- c(1,1,3:1,1:4,3), y & ...
- 配置.net程序集搜索路径
默认情况下,.net程序对外部程序集dll的搜索路径是exe文件所在的目录,虽然这种方式没有什么太多不好的地方,但是当我们引用外部程序集较多的时候显得非常杂乱.一种比较常用的解决方式是通过配置在app ...
- Jquery的Split二次分割
<script type="text/javascript">var str="Q1#k1:Q2#k2"var str2=str.split(':' ...
- 一个页面如何放多个百度编辑器 Ueditor 1.4.3?PHP如何获取Ueditor 的值?
问题1:一个页面如何放置多个Ueditor? 参考代码如下: <form method="post" action="save.php"> < ...