Android进程间通信 AIDL Service
一、服务端结构
服务端需要处理的有:
1.处理对象,对象内方法,注意:AIDL 只能对函数起作用;
package com.example.aidl;
interface Person {
String getUserName(String someone);
}
2.处理服务,在服务中绑定(onBind)和解绑(onUnbind)以及其他生命周期的方法中处理
package com.example.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.example.aidl.Person;
public class AIDLService extends Service {
private static final String TAG ="AIDLService";
//处理对象数据,逻辑控制等操作
Person.Stub personStub= new Person.Stub() {
@Override
public String getUserName(String someone) throws RemoteException {
Log.i(TAG, "getUserName");
return "getUserName:"+someone;
}
};
// 方法中切记 ,返回的一定是 Binder 对象
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind");
return personStub;
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
}
3.配置文件(android:exported="true")
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aidldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<permission android:protectionLevel="normal" android:name="permission.com.example.service.AIDLService"></permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.service.AIDLService"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.AIDLService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
二、客户端 配置
1.对象,切记,与服务端的对象完全一致,包括包名
2.调用
package com.example.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 android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import com.example.aidl.Person;
public class MainActivity extends Activity implements OnClickListener{
private Person person;
private ServiceConnection connection= new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
person = Person.Stub.asInterface(service);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.btn1)
{
Intent intent= new Intent("android.intent.action.AIDLService");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}else if(v.getId()==R.id.btn2)
{
try {
Log.i("onClick", person.getUserName("qubian"));
} catch (RemoteException e) {
e.printStackTrace();
}
}else if(v.getId()==R.id.btn3)
{
unbindService(connection);
}
}
}
http://download.csdn.net/detail/naibbian/8719687