Android AIDL 小结
1、AIDL (Android Interface Definition Language )
2、AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 Messenger ,如果不需要IPC 可以使用Binder
3、AIDL语法:基础数据类型都可以适用,List Map等有限适用。static field 不适用。
4、AIDL基本用法
第一步:实现.aidl文件
接口描述文件
、导入的包名
、如果有使用Object对象,需要该对象 implement Parcelable 接口,并且需要导入该接口包名+类名;
如果是primitive type 不需要这步。
、定义方法名称。
、所有的.aidl文件已经需要传递的对象接口需要在Service 与Client中各一份 package com.aidl;
import com.aidl.Data;
interface IaidlData
{
int getPid(); String getName(); com.aidl.Data getData();
}
2、在Service中实现.aidl 接口。实际实现的接口是在 gen中自动生成的 IaidlData.java的抽象内部类 Stub
、需要在配置文件Androidmanifest.xml文件中声明Service,并且添加intent-filter 节点 的action属性,
此属性一般可以使用 aidl的包名+文件名(因为该值在服务器与客户端一致)
、需要实现IaidlData.aidl文件中定义的接口。
aidl文件是一个接口描述文件,会在gen中自动生成一个同名的IaidlData.java接口文件,该接口文件包含一个抽象类stub,其继承了android.os.Binder、实现IaidlData接口 故,我们实际需要实现的是Stub抽象类。 public class AidlService extends Service
{
public void onCreate()
{
Log.d("aidl", "aidlService--------------onCreate");
} public IBinder onBind(Intent intent)
{
return mBinder;
} private final IaidlData.Stub mBinder = new IaidlData.Stub()
{
public int getPid()
{
return Process.myPid();
} public String getName() throws RemoteException
{
return "go or not go is a problem";
} public Data getData() throws RemoteException
{
Data data = new Data();
data.id = Process.myPid();
data.name = "go or not go is a problem";
return data;
}
};
}
3、绑定Service ,并且获取IaidlService 对象
、建立连接,使用Action属性定位需要的Service
actoin的属性的采用aidl文件的类名+包名(与服务一致),之前需要在服务中设置相同的action属性,否则找不到服务。 、获取服务返回的stub对象,mIaidlData = IaidlData.Stub.asInterface(service); package com.clent; import com.aidl.IaidlData; 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.util.Log;
import android.view.View; public class AidlClientActivity extends Activity
{
IaidlData mIaidlData; public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} protected void onStart()
{
super.onStart();
Log.d("aidl" , "onstart ----------bindservice-----"+IaidlData.class.getName());
Intent intent = new Intent(IaidlData.class.getName());
bindService(intent, mSecondaryConnection, BIND_AUTO_CREATE);
} private ServiceConnection mSecondaryConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
Log.d("aidl", "onServiceConnected----------------");
mIaidlData = IaidlData.Stub.asInterface(service);
} public void onServiceDisconnected(ComponentName className)
{
mIaidlData = null;
}
}; public void onClick(View view)
{
System.out.println( " onclick--------------- : ");
if(mIaidlData != null)
{
try
{
System.out.println( " name : "+mIaidlData.getName()); System.out.println( " id : "+mIaidlData.getPid()); System.out.println( " data : "+mIaidlData.getData().id +" "+mIaidlData.getData().name);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
} protected void onDestroy()
{
super.onDestroy();
unbindService(mSecondaryConnection);
}
}
4、如果aidl文件中需要传递Object对象,需要添加对应的.aidl文件
、定义该对象Data,并实现Parcelable
、添加Data.aidl文件,并采用如下方式编写导入Data
、需要在引用到Data的.aidl文件中 import com.aidl.Data
、需要在服务端和 客户端都添加 Data.aidl与 Data.java文件 并且需要一致。 Data.aidl
aidl文件:
package com.aidl;
parcelable Data;
5、添加 对应的Object类,并且实现Parcelable接口
public class Data implements Parcelable
{
public int id;
public String name; public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>()
{
public Data createFromParcel(Parcel in)
{
return new Data(in);
} public Data[] newArray(int size)
{
return new Data[size];
}
}; public Data()
{
} private Data(Parcel in)
{
readFromParcel(in);
} public void readFromParcel(Parcel in)
{
id = in.readInt();
name = in.readString();
} public int describeContents()
{
return ;
} public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(id);
dest.writeString(name);
}
}
Android AIDL 小结的更多相关文章
- Android AIDL自动生成Java文件测试
/******************************************************************************** * Android AIDL自动生成 ...
- Using self-defined Parcelable objects during an Android AIDL RPC / IPC call
Using self-defined Parcelable objects during an Android AIDL RPC / IPC call In my previous post “Usi ...
- AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)
首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...
- (转载)你真的理解Android AIDL中的in,out,inout么?
前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了——或者并没有,但所占篇幅甚小,且基本上都是 ...
- Android AIDL使用详解_Android IPC 机制详解
一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...
- Android AIDL的用法
一.什么是AIDL服务 一般创建的服务并不能被其他的应用程序访问.为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Cal ...
- android aidl
参考: http://blog.csdn.net/u014614038/article/details/48399935 本文提供了一个关于AIDL使用的简单易懂的例子,分为客户端和服务端两部分,分别 ...
- android aidl 进程间通信需要注意的地方(android.os.TransactionTooLargeException)
转自:http://blog.sina.com.cn/s/blog_4e1e357d0102wau9.html 1.bus工程实现通过service实现aidl实体类 2.actor工程通过发起bin ...
- AIDL小结
AIDL : Android Interface Define Language(接口定义语言) Service中跨进程间通信利器.... 一般都会有Client端和Server端(Server端提供 ...
随机推荐
- 将 php 转换/编译为 EXE
将 php 转换/编译为 EXE 本文仅仅是将原文用谷歌作了翻译,原文来源于 http://stackoverflow.com 资料来源 http://stackoverflow.com/quest ...
- js02 变量数据类型
变量 JavaScript 是一种弱类型的脚本语言 var c = 3:即变量的声明(变量使用之前必须加var声明,编程规范) 变量的命名规则! 1.变量命名必须以字母或是下标符号”_”或者”$”为开 ...
- php中ajax使用实例
php中ajax使用实例 一.总结 1.多复习:这两段代码都挺简单的,就是需要复习,要多看 2.ajax原理:ajax就是部分更新页面,其实还在的html页面监听到事件后,然后传给服务器进行操作,这里 ...
- 74.sscanf数据扫描
"%[0-9A-Za-z] 读取一个集合,遇到不是数组或者大小写字母跳出 %*[^0-9A-Za-z]读取所有的非数字字母的字符,忽略 示例: ]= "123sadsadasd ...
- Appium_pytest fixture的使用
一.定义fixture方法 # -*- coding:utf-8 -*-import pytestfrom baseutil.DriverUtil import DriverConfig @pytes ...
- 【Uva 11080】Place the Guards
[Link]: [Description] 一些城市,之间有道路相连,现在要安放警卫,警卫能看守到当前点周围的边,一条边只能有一个警卫看守,问是否有方案,如果有最少放几个警卫. [Solution] ...
- PatentTips - Method to manage memory in a platform with virtual machines
BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment ...
- drawable-实现图片旋转
今天因为需要,所以要让一个图片随着某种需要进行旋转.但是,又不能一张张的做动态图片.所以就在网上找了这么个方法.但是,这个方法有个问题,就是虽然能实现图片的旋转.但是,图片旋转以后会进行缩放.具体原因 ...
- 10. ZooKeeper之搭建伪集群模式。
转自:https://blog.csdn.net/en_joker/article/details/78673456 在集群和单机两种模式下,我们基本完成了分别针对生产环境和开发环境ZooKeeper ...
- leetcode 113. Path Sum II (路径和) 解题思路和方法
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...