Android(java)学习笔记175:Android进程间通讯(IPC)之AIDL
一、IPC
inter process communication 进程间通讯
二、AIDL
android interface defination language 安卓接口定义语言
满足两个进程之间 接口数据的交换(ipc)
首先我们搞清楚两个概念 远程服务和本地服务 ?
本地服务:服务的代码在应用程序工程的内部
远程服务:服务的代码在另一个应用程序的里面
三、下面通过案例说明AIDL(IPC)在远程服务中使用
1.首先创建一个Android项目,命名为"远程服务";
(1)工程一栏表如下:
(2)既然是远程服务,我们就先创建远程服务为RemoteServiceDemo.java,同时也需要在AndroidMainfest.xml的清单文件中注册,如下:
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.himi.remoteservice"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.himi.remoteservice.RemoteServiceDemo">
<intent-filter >
<action android:name="com.himi.remoteservice"/>
</intent-filter>
</service>
</application> </manifest>
这里我们定义了service的action,这是因为倘若应用程序自己调用服务,直接使用显式意图即可,也就是如下这种形式:
Intent intent = new Intent(this,Service.class);
startActivity(intent);
但是,我们这里是自己定义的远程服务程序,也就是这个程序在远程,让本地(或者其他用户)去调用的,所以这里使用了隐式意图。
与此同时,我们编写RemoteServiceDemo.java代码如下:
package com.himi.remoteservice; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class RemoteServiceDemo extends Service { private class MyBinder extends Binder implements IService { public void callMethodInService() {
methodInservice(); } } @Override
public IBinder onBind(Intent intent) {
System.out.println("远程的服务被绑定 onBind");
return new MyBinder();
} @Override
public boolean onUnbind(Intent intent) {
System.out.println("远程的服务被解除绑定 onUnbind");
return super.onUnbind(intent);
} @Override
public void onCreate() {
System.out.println("远程的服务onCreate");
super.onCreate();
} @Override
public void onDestroy() {
System.out.println("远程的服务onDestroy");
super.onDestroy();
} public void methodInservice() {
System.out.println("我是远程服务里面的方法,我被调用了");
}
}
这里我们定义的methodInservice(),是远程服务中的方法,这也是本地用户(或者其他用户)希望调用和访问的方法。
现在我们的需求,就是可以由别的用户程序调用这个methodInservice()方法。
上面接口IService.java为如下:
package com.himi.remoteservice; public interface IService { public void callMethodInService();
}
(3)其他的MainActivity和activity_main布局文件我们这里没有修改
(4)这样一个远程的服务就搭建好了
工程一览表如下:
2.接下来我们再新建一个Android项目,命名为" 调用远程服务的方法 ",如下:
(1)项目一览表:
(2)先编写布局文件activity_main.xml,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.himi.remotecall.MainActivity" > <Button
android:onClick="bind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="绑定远程服务" />
<Button
android:onClick="call"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="调用远程服务方法" /> </LinearLayout>
布局效果如下:
(3)实现一下MainActivity里面的代码,如下:
package com.himi.remotecall; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View; public class MainActivity extends Activity {
private MyConn conn; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} /**
* 绑定远程服务
* @param view
*/
public void bind(View view) {
Intent service = new Intent();
service.setAction("com.himi.remoteservice");
conn = new MyConn();
bindService(service, conn, BIND_AUTO_CREATE);
} private class MyConn implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder service) { } public void onServiceDisconnected(ComponentName name) { } } public void call(View view) { }
}
这个客户端访问远程服务的框架,我们搭建好了;
但是我们不能访问远端的服务方法,因为这里的
public void onServiceConnected(ComponentName name, IBinder service),这个方法是绑定服务成功的时候调用的,Service.java会反馈一个IBinder service的信息,但是我们这里并不像远程服务那样,具备这个IService的接口,通过接口类型转化,也就是如下:
IService iService = (IService)service;
iService.callMethodInService();
现在不能这样调用methodInservice()方法,因为根本就不能接收到 IBinder service
所以不能调用远程服务的methodInservice()方法。
3.该怎么办?如何调用远程服务方法?这里要利用到AIDL(IPC)
(1)第一步,我们回到"远程服务”的项目,如下:
IService.java文件在工程目录下的路径,如下:
找到这个路径如下:
看到上面的IService.java文件了,这里我们修改它的扩展名,由java 改成 aidl,如下:
回到"远程服务"这个工程,刷新它,结果如下:
这会我们发现这里的IService.java变成了IService.aidl,但是报错了,不用担心我们慢慢修改错误;
(2)来到IService.aidl文件下,如下:
aidl作为两个进程之间的接口,当然是共有的,不是共有的无法互相通信了,这里aidl中没有权限修饰符,所以删除上下两个public,结果如下:
现在IService.aidl文件也就不报错了;
(3)接着,我们来到RemoteServiceDemo.java如下:
修改成如下形式,即可:
为什么这样修改?
相应我们在gen目录下,生成一个IService.java,如下:
因为IService.Stub在gen目录下生成的IService.java的实现了接口com.himi.remoteservice.IService,同时继承了android.os.Binder,如下:
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: C:\\Users\\Administrator.GDIZOK2X2LA0SQG\\workspace\\远程服务\\src\\com\\himi\\remoteservice\\IService.aidl
*/
package com.himi.remoteservice;
public interface IService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.himi.remoteservice.IService
{
private static final java.lang.String DESCRIPTOR = "com.himi.remoteservice.IService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.himi.remoteservice.IService interface,
* generating a proxy if needed.
*/
public static com.himi.remoteservice.IService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.himi.remoteservice.IService))) {
return ((com.himi.remoteservice.IService)iin);
}
return new com.himi.remoteservice.IService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_callMethodInService:
{
data.enforceInterface(DESCRIPTOR);
this.callMethodInService();
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.himi.remoteservice.IService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public void callMethodInService() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_callMethodInService, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_callMethodInService = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public void callMethodInService() throws android.os.RemoteException;
}
(4)这样"远端服务",我们就修改好了,如下洁白无瑕:
4. 接着回到"调用远程服务的方法”这个项目下:
(1)在src目录下,创建一个包名,这个包名要 和 远程服务下的RemoteServiceDemo.java的包名一致, 这里都是com.himi.remoteservice,如下:
(2)把工程"远程服务"中的IService.aidl 复制到 工程”调用远程服务的方法"的src/com.himi.remoteservice,如下:
工程”调用远程服务的方法"的gen目录下也生成了IService.java
(3)回到工程”调用远程服务的方法"的MainActivity,编写代码如下:
package com.himi.remotecall; import com.himi.remoteservice.IService; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View; public class MainActivity extends Activity {
private MyConn conn;
private IService iservice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} /**
* 绑定远程服务
* @param view
*/
public void bind(View view) {
Intent service = new Intent();
service.setAction("com.himi.remoteservice");
conn = new MyConn();
bindService(service, conn, BIND_AUTO_CREATE);
} private class MyConn implements ServiceConnection { public void onServiceConnected(ComponentName name, IBinder service) {
iservice = IService.Stub.asInterface(service);
} public void onServiceDisconnected(ComponentName name) { } } public void call(View view) {
try {
iservice.callMethodInService();
} catch (RemoteException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
(4)布署程序到模拟器上演示:
首先布署"远端服务" 到模拟器上,如下:
最小化 "远程服务", 然后布署" 调用远程服务的方法"到模拟器上,如下:
接下我们点击 按钮----绑定远程服务 ,观察logcat打印的日志,如下:
这两条日志是 工程"远程服务"中的RemoteServiceDemo中打印的;
我们再点击多次这个 按钮----调用远程服务的方法 ,在观察logcat打印的日志,如下:
这两条日志是 工程"调用远程服务的方法"中的MainActivity中call点击事件,调用工程“远程服务”中RemoteServiceDemo的methodInservice()方法打印的;如下:
四、使用 AIDL 远程服务绑定调用的步骤:
()采用bind的方法绑定开启服务。
Intent intent = new Intent();//隐式的意图
intent.setAction("action");
bindService(intent, conn, BIND_AUTO_CREATE);
() .java的接口文件改成.aidl文件,删除public 访问修饰符。
() 在工程目录gen目录下会自动编译生成IService.java的接口文件
生成的IService.java,是通过aidl文件生成的。服务的中间人想暴露什么方法,就怎么定义接口
() 远程服务代码为private class MyBinder extends IService.Stub
同时在返回代理人对象,如下:
public IBinder onBind(Intent intent){……}
()实现ServiceConnection接口里面的方法
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IService.Sub.asInterface(service);
System.out.println("Activity,得到代理人对象");
}
注意:iService = IService.Stub.asInterface(service)
() iService.callMethodInService();
最新文章
- asp.net mvc 自定义pager封装与优化
- 组合数学 + STL --- 利用STL生成全排列
- kmp算法详解
- iOS 键盘隐藏
- Utility
- eval函数的工作原理
- Nginx问题总汇
- sqlserver 2008表分区操作
- Android应用开发基础篇(9)-----SharedPreferences
- 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例
- MongoDB和MySQL的区别
- Java多线程中join方法详解
- Python类变量和实例变量
- Android studio的错误:radle sync failed: Cause: failed to find target android-21 :
- 弹性盒模型flex
- P4306 [JSOI2010]连通数
- Mybatis源码分析之Cache一级缓存原理(四)
- webpack快速入门——配置JS压缩,打包
- sql server 2005/2008R2 报“红叉”错,即“不允许所请求的注册表访问权”的错误
- objc中类方法里的self指的是什么
热门文章
- CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
- iOS设备闪光灯控制
- Watir: Watir webdriver对JS 弹出框的操作现在非常简单。
- bzoj1911 [Apio2010]特别行动队——斜率优化DP
- Synchronized之三:Synchronized与线程中断、线程wait
- 1. jsp中<;base target=&#39;&#39; />;标签用法
- Java泛型简明教程
- fileupload简单使用
- 个人项目开发PSP实践-MyWCprj
- qq教xixi写模拟加法【非常爆炸】