RPC:進程間通訊,
AIDL:android interface definition language 安卓接口定義語言,
在版本4.0之前使用時可以直接使用隱式意圖去綁定service,但是在4.0之後到去使用就先得把隱式意圖轉爲顯示意圖才能調用另一個進程裏的方法
需要注的事項:
1.包名,aidl名稱必須一致
終端:
第一步:定義aidl接口:
interface AlipayRemoteService {
boolean forwardPayMoney(float money);
}
第二步:寫一個服務回調
//服務裏面的方法
public class AlipayService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new PayController();//返回一個Binder對象
}
//回調時使用的方法
public boolean pay(float money) {
Toast.makeText(getApplicationContext(), "支付" + money + "成功",
Toast.LENGTH_SHORT).show();
return true;
}
//Stub裏面自動生成接口裏的類
public class PayController extends Stub {
@Override
public boolean forwardPayMoney(float money) throws RemoteException {
return pay(money);
}
}
}
第三步:在清單文件裏面註冊Service並指定動作爲隱式意圖
<service android:name="com.example.rpcdemo.service.AlipayService" >
<intent-filter>
<action android:name="com.ciat.alipay" >
</action>
</intent-filter>
</service>
第四步:開戶服務 :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i=new Intent(this,AlipayService.class);
startService(i);
}
}
2.android4.0版本以後不支持隱士意圖,需要轉換成顯示意圖
客戶端:
interface AlipayRemoteService {
boolean forwardPayMoney(float money);
}
第一步:創建並綁定意圖對象,這裏需要把隱式意圖轉換成顯示意圖
Intent service = new Intent();
service.setAction("com.ciat.alipay");//因爲service在另一個客戶裏面,所以只能使用的是隱式意圖
final Intent eintent = new Intent(createExplicitFromImplicitIntent( this, service));//然後隱式版本4.0以後不能使用,所以要先轉爲顯示意圖後能才使用
boolean bindService = bindService(eintent, new MyConnection(),BIND_AUTO_CREATE);//通過續寫服務來獲取終端裏面的方法
if (bindService) {
Toast.makeText(this, "服務綁定成功", 1).show();
} else {
Toast.makeText(this, "服務綁定失敗", 1).show();
}
第二步:實現服務連接通道
//獲取終端服務裏的方法
class MyConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(getApplicationContext(), "服務已經連接...",
Toast.LENGTH_SHORT).show();
remoteService = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(getApplicationContext(), "服務已經斷開...",
Toast.LENGTH_SHORT).show();
}
}
第三步:轉換意圖對象
/**
* 隱士意圖轉換爲顯示意圖方法
* @param context
* @param implicitIntent
* @return Intent
*/
public static Intent createExplicitFromImplicitIntent(Context context,
Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent,
0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
第四步:回調終端方法
//調用終端方法轉參
public void pay(View v) {
if (TextUtils.isEmpty(et.getText().toString().trim())) {
Toast.makeText(this, "請輸入金錢數", 0).show();
return;
}
Float money = Float.valueOf(et.getText().toString().trim());
try {
remoteService.forwardPayMoney(money);
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(this, "付款失敗", 1).show();
return;
}
Toast.makeText(this, "成功轉賬:" + money + "元!", 0).show();
}