Android Interface Definition Language(AIDL)
Android接口定义语言,Android中,一个进程是无法正常的访问另外一个进程的内容的。
众所周知,Android中用Handler进行线程间的交互,但是进程和线程是不一样的,每个进程CPU都会给他分配一定的内存空间,而每个进程中可能包含一个或者多个线程,所以线程间交互要容易的多。进程间想要通信需要将数据对象解析成元操作系统可识别的数据,因为进程是在占据一块内存空间,所以数据还要穿过进程的边缘。如果用代码实现很繁琐,而在Android中提供了AIDL给开发者使用。
创建一个使用AIDL的Service,需要以下三步
1、创建一个.aidl 文件
2、实现接口
3、向客户端展示接口
创建一个.aidl 文件
创建一个aidl文件,adb会自动在gen文件夹下生成对应的接口。
package com.example.demo.aidl;
interface Hellow{
void say(String str);
String talk();
}
如下图,可以看到这个接口的结构。
实现接口,向客户的展示
接口已经生成,我们先建一个Service来实现这个接口。
package com.example.demo.service;
import com.example.demo.aidl.Hellow;
import com.example.demo.aidl.Hellow.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
/**
* @project HelloAIDL
* @ClassName AIDLService.java
* @Description
* @author xugang
* @date 2015-9-10 上午11:10:36
*/
public class AIDLService extends Service {
@Override
public IBinder onBind(Intent intent) {
return stub;
}
public boolean onUnbind(Intent intent) {
return true;
};
public void onDestroy() {
System.out.println("======AIDLService>onDestroy()");
};
Hellow.Stub stub = new Stub() {
@Override
public String talk() throws RemoteException {
return "AIDLService to talk=="+System.currentTimeMillis();
}
@Override
public void say(String str) throws RemoteException {
System.out.println("======AIDLService>say:"+str);
}
};
}
记得在注册文件中注册,不然不能使用:
<service android:name="com.example.demo.service.AIDLService">
<intent-filter >
<action android:name="com.example.demo.AIDLService"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
OK,后台服务已经准备就绪,现在写个Activity,就可以进行交互了。
Activity&Service 交互
新建一个Activity,这里需要一个ServiceConnection 对象。
package com.example.demo;
import com.example.demo.aidl.Hellow;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
public class MainActivity extends Activity implements OnClickListener {
private String ACTION = "com.example.demo.AIDLService";
private Hellow hellow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
}
private void setEnabled(int id){
switch (id) {
case R.id.button1:
findViewById(R.id.button1).setEnabled(false);
findViewById(R.id.button2).setEnabled(true);
findViewById(R.id.button3).setEnabled(true);
findViewById(R.id.button4).setEnabled(true);
break;
case R.id.button2:
findViewById(R.id.button1).setEnabled(true);
findViewById(R.id.button2).setEnabled(false);
findViewById(R.id.button3).setEnabled(false);
findViewById(R.id.button4).setEnabled(false);
break;
default:
break;
}
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
hellow = Hellow.Stub.asInterface(service);
}
};
@Override
public void onClick(View v) {
setEnabled(v.getId());
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(ACTION);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
break;
case R.id.button2:
unbindService(connection);
break;
case R.id.button3:
//从Service获取数据
try {
String s = hellow.talk();
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.button4:
//向Service提供数据
try {
hellow.say("=="+System.currentTimeMillis());
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
}
OK,就是这样,没啥内容
Demo:http://download.youkuaiyun.com/detail/hello_12413/9094607
效果如下图所示: