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.csdn.net/detail/hello_12413/9094607
效果如下圖所示: