我們假定兩個應用程式為A和B。
我的意圖是在B應用工程中建立Aidl,然後在這裡實現 aidl裡面的方法,然後在A應用工程中去遠程調用:
好了:我先去建立 B應用程式:package com.king.android.service;
interface IService{
//設定地址
void setAddress(String address);
//設定國家
void setCountry(String country);
String disPlay();
}
在Aidl程式中不要任何存取修飾詞,也不要 static,final 來修飾 ,另外其它的資料類型會有所不同,後期再說:
好了,建立完這個aidl檔案,然後就需要我去去實現這個aidl介面的方法:
package com.king.android.service;
import android.os.RemoteException;
/**
* 描述:實現IService介面
* 作者:Andy.Liu
* 時間: 2012-7-30 上午08:41:02
**/
public class IServiceImpl extends IService.Stub{
private String mAddress = null;
private String mCountry = null;
@Override
public void setAddress(String address) throws RemoteException {
this.mAddress = address;
}
@Override
public void setCountry(String country) throws RemoteException {
this.mCountry = country;
}
@Override
public String disPlay() throws RemoteException {
return "address:"+mAddress+"--------------mCountry:"+mCountry;
}
}
上面是實現這個aidl介面的實作類別。
把自己的aidl暴露出來,供別的應用程式使用:
package com.king.android.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.king.android.service.IService.Stub;
/**
* 描述:將實現的aidl介面暴露給用戶端
* 作者:Andy.Liu
* 時間: 2012-8-2 上午07:01:49
**/
public class MyRemoteSerivce extends Service {
public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";
private Stub iService = new IServiceImpl();
@Override
public IBinder onBind(Intent intent) {
return iService;
}
}
另外需要在Androidmenifest.xml檔案中去註冊這個服務。以及過濾。。。
這樣在B程式程式中的準備工作做完了。。。
A工程中準備工作:
直接把B應用程式中aidl檔案所在的包以及,這個Aidl檔案copy到 A應用程式中。並儲存。。。
好了,這樣我們就可以去在A應用程式中調用B應用程式提供的介面方法。。。
package com.king.android.controls;
import com.king.android.R;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";//服務指定的動作
private IService iService;
private Button btn;
MyReceiver2 receiver = new MyReceiver2();
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
//執行個體化MYReceiver2
registerReceiver(receiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn_voice);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(MY_SERVICE);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
}
});
}
private ServiceConnection conn = new ServiceConnection() {
@Override
synchronized public void onServiceConnected(ComponentName name, IBinder service) {
iPerson = IPerson.Stub.asInterface(service);
if(null!=iPerson){
//RPC調用方法
try {
iPerson.setAddress("shenzhen.guangdong");
iPerson.setCountry("china");
String msg = iPerson.display();
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
在onServiceConnedted中傳入需要的參數。
然後去綁定這個服務即可。