AIDL 简单实现
实现步骤
1.建立一个aidl文件,在里面定义好接口,注意里面不能写public修饰符,同接口一样,包名要一致.
package com.systemset.aidl;
interface ILight {
int getScreenBrightness();
void setScreenBrightness(int value);
int getScreenOffTimeout();
void setScreenOffTimeout(int value);
}
2.实现服务端进程,提供对应的功能.
Build生成对应的java文件(自动生成的java文件在/gen/yourPackage中),里面包含了一个内部类Stub,写一个Service子类,并在内部继承Stub类,实现对应的功能.
// ILight.java
package com.systemset.aidl;
public interface ILight extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.systemset.aidl.ILight
{
private static final java.lang.String DESCRIPTOR = "com.systemset.aidl.ILight";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.systemset.aidl.ILight interface,
* generating a proxy if needed.
*/
public static com.systemset.aidl.ILight asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.systemset.aidl.ILight))) {
return ((com.systemset.aidl.ILight)iin);
}
return new com.systemset.aidl.ILight.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_getScreenBrightness:
{
data.enforceInterface(DESCRIPTOR);
int _result = this.getScreenBrightness();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_setScreenBrightness:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
this.setScreenBrightness(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getScreenOffTimeout:
{
data.enforceInterface(DESCRIPTOR);
int _result = this.getScreenOffTimeout();
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_setScreenOffTimeout:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
this.setScreenOffTimeout(_arg0);
reply.writeNoException();
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.systemset.aidl.ILight
{
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 int getScreenBrightness() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getScreenBrightness, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void setScreenBrightness(int value) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(value);
mRemote.transact(Stub.TRANSACTION_setScreenBrightness, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
@Override public int getScreenOffTimeout() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getScreenOffTimeout, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public void setScreenOffTimeout(int value) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(value);
mRemote.transact(Stub.TRANSACTION_setScreenOffTimeout, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_getScreenBrightness = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_setScreenBrightness = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_getScreenOffTimeout = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_setScreenOffTimeout = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
}
public int getScreenBrightness() throws android.os.RemoteException;
public void setScreenBrightness(int value) throws android.os.RemoteException;
public int getScreenOffTimeout() throws android.os.RemoteException;
public void setScreenOffTimeout(int value) throws android.os.RemoteException;
}
// LightService.java
package com.systemset.aidl; import static android.util.Log.d;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException; import com.apical.systemset.util.SetManager; public class LightService extends Service { private static final String TAG = "debug";
private LightServer mLightServer; public LightService() {
} @Override
public IBinder onBind(Intent intent) {
if (mLightServer == null)
mLightServer = new LightServer();
d(TAG, "LightService: onBind");
return mLightServer;
} @Override
public boolean onUnbind(Intent intent) {
d(TAG, "LightService: onUnbind");
mLightServer = null;
return super.onUnbind(intent);
} public class LightServer extends ILight.Stub { @Override
public int getScreenBrightness() throws RemoteException {
int value = SetManager.getInstance().getScreenBrightness(
LightService.this);
d(TAG, "LightServer: call getScreenBrightness! return value is "
+ value);
return value;
} @Override
public void setScreenBrightness(int value) throws RemoteException {
d(TAG, "LightServer: call setScreenBrightness.value is " + value);
SetManager.getInstance().setScreenBrightness(LightService.this,
value);
} @Override
public int getScreenOffTimeout() throws RemoteException {
int value = SetManager.getInstance().getScreenOffTimeout(
LightService.this);
d(TAG, "LightServer: call getScreenOffTimeout! return value is "
+ value);
return value;
} @Override
public void setScreenOffTimeout(int value) throws RemoteException {
d(TAG, "LightServer: call setScreenOffTimeout.value is " + value);
SetManager.getInstance().setScreenOffTimeout(LightService.this,
value);
}
} public static int getScreenBrightness(Context context) {
int light = 0;
try {
light = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
d(TAG, "SystemManager:", e);
}
return light;
} public static void setScreenBrightness(Context context, int value) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, value);
} public static int getScreenOffTimeout(Context context) {
int time = 0;
try {
time = Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT);
} catch (SettingNotFoundException e) {
d(TAG, "SystemManager:", e);
}
return time;
} public static void setScreenOffTimeout(Context context, int value) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, value);
}
}
// 在AndroidManifest.xml中注册服务
<service
android:name="com.systemset.aidl.LightService"
android:process=":remote"
android:exported="true" >
<intent-filter>
<action android:name="com.apical.systemset.aidl.LightService" />
</intent-filter>
</service>
3.实现客户端进程,提供对应的功能.
将aidl文件拷贝到客户端进程中,注意包名需要一致.同样Build之后,会生成对应的java文件.使用bindService启动服务,在ServiceConnection的
onServiceConnected函数中使用Stub.asInterface(binder)取得对象.
Intent intent = new Intent();
intent.setAction("com.apical.systemset.aidl.LightService");
// 不能调用setPackage,否则会出现如下错误
// Unable to start service Intent { act=com.apical.systemset.aidl.LightService pkg=com.diysoul.aidlclient } U=0: not found
// intent.setPackage(getPackageName());
boolean bindResult = bindService(intent, mServiceConnection,
Context.BIND_AUTO_CREATE);
// ServiceConnection对象定义如下
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName componentName) {
d(TAG, "ServiceConnection componentName:" + componentName);
mLight = null;
}
@Override
public void onServiceConnected(ComponentName componentName,
IBinder binder) {
d(TAG, "ServiceConnection componentName:" + componentName);
mLight = ILight.Stub.asInterface(binder);
try {
int screenBrightness = mLight.getScreenBrightness();
int ScreenOffTimeout = mLight.getScreenOffTimeout();
d(TAG, "client getScreenBrightness = " + screenBrightness);
d(TAG, "client getScreenOffTimeout = " + ScreenOffTimeout);
} catch (RemoteException e) {
d(TAG, "", e);
}
}
};
// MainActivity.java
package com.diysoul.aidlclient; 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 static android.util.Log.d;
import com.systemset.aidl.ILight; public class MainActivity extends Activity { private static final String TAG = "debug";
private ILight mLight; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Intent intent = new Intent();
intent.setAction("com.apical.systemset.aidl.LightService");
// Unable to start service Intent { act=com.apical.systemset.aidl.LightService pkg=com.diysoul.aidlclient } U=0: not found
// intent.setPackage(getPackageName());
boolean bindResult = bindService(intent, mServiceConnection,
Context.BIND_AUTO_CREATE);
d(TAG, "bindResult:" + bindResult);
d(TAG, "mLight:" + mLight);
} @Override
protected void onDestroy() {
super.onDestroy();
unbindService(mServiceConnection);
} private ServiceConnection mServiceConnection = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName componentName) {
d(TAG, "ServiceConnection componentName:" + componentName);
mLight = null;
} @Override
public void onServiceConnected(ComponentName componentName,
IBinder binder) {
d(TAG, "ServiceConnection componentName:" + componentName);
mLight = ILight.Stub.asInterface(binder);
try {
int screenBrightness = mLight.getScreenBrightness();
int ScreenOffTimeout = mLight.getScreenOffTimeout();
d(TAG, "client getScreenBrightness = " + screenBrightness);
d(TAG, "client getScreenOffTimeout = " + ScreenOffTimeout);
} catch (RemoteException e) {
d(TAG, "", e);
}
}
};
}
AIDL 简单实现的更多相关文章
- AIDL简单使用
1.AIDL定义 AIDL是android interface definition language的缩写,它对android IPC组件Binder进行了封装.使用它不需理会底层IPC的实现,只需 ...
- Android的IPC机制(一)——AIDL的使用
综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...
- AIDL旅行记之开篇AIDL基本介绍
嗨,伙伴们,计划了一周的想法最终要在这一刻实现了. 一直都想写一个博客专栏,但是总是鼓不起勇气来写.感觉自己的水量还不太够.哈哈.这次下定决心,与小伙伴们一起分享下Android中的AIDL,从此,也 ...
- android跨进程通信(IPC)——AIDL
转载请标明出处: http://blog.csdn.net/sinat_15877283/article/details/51026711: 本文出自: [温利东的博客] 近期在看 @任玉刚 大神编写 ...
- (转载)Android中的Service:Binder,Messenger,AIDL(2)
前言 前面一篇博文介绍了关于Service的一些基本知识,包括service是什么,怎么创建一个service,创建了一个service之后如何启动它等等.在这一篇博文里有一些需要前一篇铺垫的东西,建 ...
- Android艺术开发探索——第二章:IPC机制(下)
Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...
- Android进程间通信IPC
一.IPC的说明 IPC是Inter-Process Communication的缩写,含义为进程间通信或跨进程通信,是指两个进程之间进行数据交换的过程. IPC不是Android独有的,任何一个操作 ...
- IPC 简说
IPC(inter-process communication)进程间通信 多进程分为两种情况 1. 同一个应用,使用android:process属性启动的四大组件 2. 多应用 通过android ...
- i.mx6 Android5.1.1 vibrator系统服务流程
0. 概述 0.1 小结 下面来从APP一直分析到kernel的driver,因为vibrator是我所知的最简单的系统服务,分析过程过来,可以获取整个安卓服务的运行思路,把相关知识点都串联起来,又不 ...
随机推荐
- 解决ssh_exchange_identification:read connection reset by peer 原因
服务器改了密码,试过密码多次后出现: ssh_exchange_identification: read: Connection reset by peer 可以通过ssh -v查看连接时详情 Ope ...
- 【springmvc+mybatis项目实战】杰信商贸-4.maven依赖+PO对+映射文件
上一篇我们附件的增删改查功能全部完成.但是我们的附件有一个字段叫做“类型”(ctype),这里我们要使用数据字典,所以对于这一块我们要进行修改. 首先介绍一下数据字典 数据字典它是一个通用结构,跟业务 ...
- 子序列 (All in All,UVa10340)
题目描述:算法竞赛入门经典习题3-9 题目思路:循环匹配 //没有按照原题的输入输出 #include <stdio.h> #include <string.h> #defin ...
- 洪水!(Flooded! ACM/ICPC World Final 1999,UVa815)
题目描述:竞赛入门经典的习题4-10 解题思路:1.把各个网格想象成一个数组 2.排序 3.雨水总体积去铺满 //太懒了只写了求海拔 #include <stdio.h> #define ...
- python selenium 使用htmlunit 执行测试。非图形界面浏览器。
其实就是换个浏览器,只是这个浏览器没有图形界面而已. browser = webdriver.Chrome() 换成 browser = webdriver.Remote(desired_capabi ...
- 为什么请求时,需要使用URLEncode做encode转码操作(转)
什么要对url进行encode 发现现在几乎所有的网站都对url中的汉字和特殊的字符,进行了urlencode操作,也就是: http://hi.baidu.com/%BE%B2%D0%C4%C0%C ...
- javaIO--文件操作类
文件操作类主要是使用File类的各种方法对文件和目录进行操作.包括文件名.文件长度.最后修改时间和是否只读等,提供获得当前文件的路径名.判断文件是否存在.创建.删除文件和目录等一系列的操作方法. 下面 ...
- <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>
<Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...
- OSG学习:响应键盘鼠标示例
示例功能:示例里面有两个模型,一个是牛,另一个是飞机.鼠标右键时牛和飞机都隐藏,鼠标左键双击时牛和飞机都显示,按键盘上面的LEFT键,显示牛,按键盘上面的RIGHT键显示飞机.其中显示与隐藏节点使用的 ...
- 3dContactPointAnnotationTool开发日志(八)
今天上午去实验室打算把项目从github上pull下来发现貌似不行,然后强行pull下来后项目变得乱七八糟了,有的组件都不知道去哪里了.去github上看了看发现上面day6和day7都没有,特别 ...