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可 ...
随机推荐
- C# WinForm开发 取消窗体关闭按钮
//禁用窗体的关闭按钮 private const int CP_NOCLOSE_BUTTON = 0x200; protected override CreateParams CreateParam ...
- set注入
顾名思义set注入必须要有set方法. 基本类型的注入.引用类型注入.List注入.Set注入.Map注入.Properties注入 public class person { private car ...
- HDU 1426 Sudoku Killer【DFS 数独】
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品— ...
- ubuntu下如何查找某个文件的路径
1.whereis 文件名 特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来. 2.find / -name ...
- HDOJ 4961 Boring Sum
Discription Number theory is interesting, while this problem is boring. Here is the problem. Given a ...
- [BZOJ4817]树点涂色
第一个操作比较麻烦,但可以看出它和lct里的access操作差不多,所以可以利用lct的性质巧妙维护操作1 直接用lct维护树中同颜色的链(因为染色操作是从$x$染到根所以同颜色的点一定形成一条链), ...
- 【树状数组】bzoj2789 [Poi2012]Letters
处理数组A,A[i]表示字符串a的第i个字符排序后应去的位置,队列可. 对于多次出现的字符,其在a中的顺序和在b中的顺序应该是一一对应的. #include<cstdio> #includ ...
- 10.3(Java学习笔记)JDBC时间操作
一.时间分类 数据库 java类 Date ---- java.sql.Date 表示日期 yyyy-MM--dd (年月日) Time ----java.sql.Time 表示 ...
- dwz中弹出的窗口页面如何获取前页面(点击按钮的页面)的元素???
在页面A.jsp中点击一个按钮,使用$.pdialog.open()方法弹出b.jsp页面(对话框窗口),我要在b.jsp中选中值然后关闭窗口(b.jsp)返回值给A.jsp~ =========== ...
- zk介绍
1. 配置管理 Zookeeper提供了这样的一种服务:一种集中管理配置的方法,我们在这个集中的地方修改了配置,所有对这个配置感兴趣的都可以获得变更.这样就省去手动拷贝配置了,还保证了可靠和一致性. ...