首先,为什么要用aidl
---------------------------------------------------------------------------------------------------------------------------
aidl其实就是进程间的通信
官方文档特别提醒我们何时使用AIDL是必要的:只有你允许客户端从不同的应用程序为了进程间的通信而去访问你的service,以及想在你的service处理多线程。如果不需要进行不同应用程序间的并发通信(IPC),you should create your interface by implementing a Binder;或者你想进行IPC,但不需要处理多线程的,则implement your interface using a Messenger
简单说就是,1、你某个service想被别人使用;2、你想使用其它进程的service
然后,怎么用aidl(一下是一个最简单的使用,从服务端获取一个字符串)
-------------------------------------------------------------------------
步骤:
服务端
1、在服务程序端建立xxx.aidl文件,里面放接口(这个接口要与文件名称一样),写法跟写Java程序一样
2、新建一个Service,这个Service里面实现之前那个aidl文件里面的接口
3、manifest.xml文件注册Service
client端
1、copy之前的那个aidl文件过来(要独立一个跟服务端文件夹相同名字的文件夹,否则会报错最后面的那个错误
)
2、在activity里面初始化ServiceConnection(用来bind Service的),和获得服务端的service
服务端的service = 接口.Stub.asInterface(service);//在ServiceConnection的onServiceConnected里面实现
3、bindService
实现代码:
先看一下目录结构
ExecuteMyAidlService.aidl文件(注意接口名称要与文件名前边相同)
package aid;
interface ExecuteMyAidlService {
String sayHello();
}
MyAidlService.java文件
package aidlservice;
import aid.ExecuteMyAidlService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyAidlService extends Service
{
private static final String TAG = "MyAidlService";
private ExecuteMyAidlService.Stub mBinder = new ExecuteMyAidlService.Stub() {
@Override
public String sayHello() throws RemoteException {
// TODO Auto-generated method stub
return "hello ";
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return mBinder;
}
private void Log(String str) {
android.util.Log.d(TAG, "------ " + str + "------");
}
@Override
public void onCreate() {
Log("service create");
}
@Override
public void onStart(Intent intent, int startId) {
Log("service start id=" + startId);
}
}
activity里面没有东西就不贴了
客户端的目录结构
ClientActivity的代码:
package com.example.aidlclienttest;
import aid.ExecuteMyAidlService;
import android.support.v7.app.ActionBarActivity;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ClientActivity extends ActionBarActivity {
private ExecuteMyAidlService mIaidlServerService = null;
private TextView mTextView = null;
private Button mButton = null;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mIaidlServerService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
mIaidlServerService = ExecuteMyAidlService.Stub.asInterface(service);
//aidl通信
try {
String mText = "Say hello: " + mIaidlServerService.sayHello();
mTextView.setText(mText);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
mTextView = (TextView)findViewById(R.id.helloword);
mButton = (Button)findViewById(R.id.getServiceFromAidl);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent service = new Intent("aidl.ExecuteMyAidlService");
service.setAction("aidl.ExecuteMyAidlService");
bindService(service, mConnection,BIND_AUTO_CREATE);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.client, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里就实现了最简单的aidl通信的过程
看一下效果
先开启服务端
然后再启动client端,点击bindservice之后
进阶aidl
---------------------------------------------------------------------------------------------
1、如果我想从service获取对象呢?(最要应用)
将对象实现parcelable 接口
为这个对象写个aidl文件[里面只需要定义一下这个对象就行了如:]
parcelable Object
还是看一下代码吧
先看一下这个结构
增加一个Person的类,这个类要实现
parcelable 接口
代码
package aid;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
public class Person implements Parcelable{
private String name;
private int age;
private String place;
public Person(Parcel source) {
// TODO Auto-generated constructor stub
name = source.readString();
age = source.readInt();
place = source.readString();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", place=" + place
+ "]";
}
public Person() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
dest.writeString(place);
}
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>(){
@Override
public Person createFromParcel(Parcel source) {
// TODO Auto-generated method stub
Person person = new Person(source);
return person;
}
@Override
public Person[] newArray(int size) {
// TODO Auto-generated method stub
return new Person[size];
}};
}
同样也要写一个aidl文件
想之前的那个接口里面增加一个获取对象的空函数
在service里面实现,步骤跟之前的类似不多叙述了
这样在客户端那边就可以获取到这个对象了
2、如果activity要往service里面传递数据呢?
【如果是对象一样是要parcelable,(因为client和server的aidl文件是一样的)】
要在接口函数那里加上in
基本类型(int,long,char,boolean等),String,CharSequence,List,Map,其他类型必须使用import导入,即使它们可能在同一个包里,比如上面的Person,尽管它和IMyService在同一个包中,但是还是需要显示的import进来。
另外,接口中的参数除了aidl支持的类型,其他类型必须标识其方向:到底是输入还是输出抑或两者兼之,用in,out或者inout来表示,一般in标记,因为大多数情况下输入型参数。
另外,接口中的参数除了aidl支持的类型,其他类型必须标识其方向:到底是输入还是输出抑或两者兼之,用in,out或者inout来表示,一般in标记,因为大多数情况下输入型参数。
这个也很简单:
过程是某个client这边向serverSevice传入一个对象,service对这个对象处理完,通知或者返回client
贴一下关键的代码
client端
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mIaidlServerService = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
mIaidlServerService = ExecuteMyAidlService.Stub.asInterface(service);
//aidl通信
try {
String mText = "Say hello: " + mIaidlServerService.sayHello();
Person mPerson = mIaidlServerService.getDefaultPerson();
System.out.println(mPerson);
mPerson.setAge(mPerson.getAge()+1);
<span style="white-space:pre"> </span>//这里向service传入对象
mIaidlServerService.tellMeHowOldAmI(mPerson,mClientCallBack);
mTextView.setText(mText);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
这边要实现callback
ClientCallBack.Stub mClientCallBack = new ClientCallBack.Stub() {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void handleByServer(String param) throws RemoteException {
<span style="white-space:pre"> </span>// TODO Auto-generated method stub
<span style="white-space:pre"> </span>Toast.makeText(getApplicationContext(), param, Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>};
服务端
@Override
public String tellMeHowOldAmI(Person person,ClientCallBack mClientCallBack) throws RemoteException {
// TODO Auto-generated method stub
mCallBack = mClientCallBack;
mCallBack.handleByServer("your age is "+String.valueOf(person.getAge()));
return String.valueOf(person.getAge());
}
};
过程中碰到的错误
------------------------------------------------------------------------------------------------------------------------------------------------