一、整体工程图
二、首先创建在两个工程中都创建RemoteWebPage.aidl,然后分别在gen目录下生成了RemoteWebPage.java
package com.braincol.aidl.service;
interface RemoteWebPage {
String getCurrentPageUrl();
}
三、在RemoteService实现接口,并且在onBind方法中返回IBinder对象。
package com.braincol.aidl.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
*
* @author chenzheng_java
* @description 提供服务的service
*
*/
public class RemoteService extends Service {
private final static String TAG = "RemoteService";
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "执行了OnBind");
return new MyBinder();
}
private class MyBinder extends RemoteWebPage.Stub{
@Override
public String getCurrentPageUrl() throws RemoteException{
return "http://www.cnblogs.com/hibraincol/";
}
}
}
四、ClientActivity中通过remoteWebPage = RemoteWebPage.Stub.asInterface(service)获取到RemoteWebPage,就可以调用其中的方法getCurrentPageUrl了。
package com.braincol.aidl.client;
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 android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.braincol.aidl.service.RemoteWebPage;
public class ClientActivity extends Activity implements OnClickListener {
private final static String TAG="jltxgcy";
Button btn_bind ;
String actionName = "com.braincol.aidl.remote.webpage";
RemoteWebPage remoteWebPage=null;
String allInfo = null;
boolean isBinded=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_bind = (Button) findViewById(R.id.btn_bind);
btn_bind.setOnClickListener(this);
}
@Override
protected void onPause(){
super.onPause();
if(isBinded){
Log.d(TAG,"unbind");
unbindService(connection);
}
}
private class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "建立连接...");
remoteWebPage = RemoteWebPage.Stub.asInterface(service);
try {
Log.d(TAG, remoteWebPage.getCurrentPageUrl());
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "onServiceDisconnected...");
}
}
MyServiceConnection connection = new MyServiceConnection();
@Override
public void onClick(View v) {
if(v==this.btn_bind){
if(!isBinded){
Intent intent = new Intent(actionName);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
isBinded= true;
}
}
}
}
五、另外注意RemoteService的AndroidManifest.xml写法。
ClientActivity的AndroidManifest.xml为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.braincol.aidl.client"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ClientActivity"
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>
RemoteService的AndroidManifest.xml为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.braincol.aidl.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name="RemoteService">
<intent-filter>
<action android:name="com.braincol.aidl.remote.webpage"/>
</intent-filter>
</service>
</application>
</manifest>
代码地址:https://github.com/jltxgcy/Demo