注意点:
1.service端和client端的aidl文件所在的包名必须相同;
2.在service中实现aidl类中的Stub类。
1.两个应用的目录关系:
2.AndroidMenifest.xml
服务端:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pax.aidl.service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service
android:name="com.pax.aidl.service.RemoteService">
<intent-filter>
<action android:name="com.pax.aidl.service.remote">
</action>
</intent-filter>
</service>
</application>
</manifest>
客户端:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pax.aidl.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.pax.aidl.client.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>
</application>
</manifest>
3.aidl文件:
package com.pax.aidl.service;
interface Remote {
String getUrl();
}
4.service类:
package com.pax.aidl.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.pax.aidl.service.Remote;
public class RemoteService extends Service{
private final static String TAG = "RemoteService";
private MyBind myBind = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG,"onBind");
return myBind;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i(TAG,"onCreate");
myBind = new MyBind();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG,"onDestroy");
}
private class MyBind extends Remote.Stub{
@Override
public String getUrl() throws RemoteException {
// TODO Auto-generated method stub
return "www.baidu.com";
};
}
}
5.client Activity类:
package com.pax.aidl.client;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.util.Log;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import com.pax.aidl.service.Remote;
public class MainActivity extends Activity implements OnClickListener{
private final static String TAG = "MainActivity";
private Button bt = null;
private TextView tv = null;
private Remote remote = null;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"onCreate");
bt = (Button)findViewById(R.id.geturl_bt);
tv = (TextView)findViewById(R.id.url_text);
bt.setOnClickListener(this);
Intent intent = new Intent("com.pax.aidl.service.remote");
bindService(intent, mConnection, Context.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.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i(TAG,"onClick");
try {
url = remote.getUrl();
tv.setText(url);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG,"onDestroy");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG,"onPause");
unbindService(mConnection);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG,"onStop");
}
private ServiceConnection mConnection = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder service) {
// TODO Auto-generated method stub
Log.i(TAG,"connect service");
remote = Remote.Stub.asInterface(service);
if(remote == null){
Log.e(TAG,"remote is null");
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
remote = null;
}
};
}