android 中activity调用远程service中的方法之 aidl的使用

时间:2021-11-24 23:12:04

服务端:只有服务,没有界面

1.编写interface文件,复制到 .aidl 文件中,并去掉其中的public 等修饰符。系统会自动在gen目录下生成对应的java文件  (对应本地调用中的接口文件)

  aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkoAAAB/CAYAAAAQC+2uAAAABHNCSVQICAgIfAhkiAAAEVpJREFUeF7t3U1u5LgVAGA7yMKLDpBcN5hlkFPkHDlKtllmmQDdQPfOAQMQIZ5JilSpVPr5DAzGEsnH9z7KLo5UU37/29//8fnmiwABAgQIECBA4IvA+8/vPz8/vn18aRg58evHr7e1Y0fi60OAAAECBAgQeKXA7145ubkJECBAgAABAkcWsFE68urIjQABAgQIEHipwOE3Su/v7y8FuuPkyXzJvWzP/ZfG3NFSzQQIECBwboHhjdJvf/7tLf3ji0AU+Pz8fEv/+CJAgAABAlcTGNoopQ3SX/76l6vVrp6GgI1PA8ZpAgQIELidwO9HKh7dJMVHL/EuQ9nea0s5xfZ0Lo8v22ZilnGXcm25tMa1zrfi5PNL41r1pfPRoWZWm78XM/evxYq51mK3zuWxtbitMc4TIECAAIFXCwxtlGaTzC+G8cW89cI+8iLa6tOLOdqW6ou51mpu5VD2bdVeizcyrlXDUrxeeytmmXscH30e2TTF2I4JECBAgMBRBTbfKJUvwrHo3ovr0rhWey9mnL88XjuulUeK3Wvr5dIb18szb17iJqY3V2rrxVwau7a9V+PamMYRIECAAIFnC2y+UWolHF/MZ16s04tsHJ/miedizPI4vlDH41berzzfqy+bzObXizkbS38CBAgQIHB1gaE3c2+NEDc0KX7tXDnv0sagNj6Nyf/EGmr9Y5/a8SPj1o4d8Ym5prmW5ltqjzHz8ZpxI/m05nOeAAECBAi8SmDoT5jUPhYgvcE7/gmT+AIa79r02ltt6XwZp3Zc4uW+MV7qE+NE9JhvbE/HMW45X4wfj2MOvXh57tZ8ZXst7zyu15ZjzJql/uU6xBx7ddbyyXn4NwECBAgQOJrA0EaplXRto3SUF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CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgL/BRk9rrnZqPm2AAAAAElFTkSuQmCC" alt="" />

2.编写service,其中内部类的自定义bind 只需要继承Stub即可。(本地调用则需要继承Bind 并实现 interface接口)

 public class PayService extends Service {

     @Override
public IBinder onBind(Intent intent) {
return new Pay();
}
public class Pay extends Stub {
@Override
public void pay(String userName, String password, String sendToAccount, float money) throws RemoteException {
toPay(userName, password, sendToAccount, money);
}
}
public void toPay(String userName, String password, String sendToAccount, float money){
System.out.println(userName + "@" + password + " to " + sendToAccount + "$" + money);
} }

3.清单文件中注册,添加intent-filter 项,以便于被调用

  <service android:name="cn.pay.service.PayService" >
            <intent-filter>
                <action android:name="cn.pay.service.PayService" />
            </intent-filter>
        </service>

========================================================分隔线========================================================

客户端:

  在activity中自定义ServiceConnection的内部类。转化接口实例的方法用 接口类名.Stub.asInterface(service)  (本地调用是直接将service强转为接口实例)

 

 package com.example.aidlpayclient;

 import cn.pay.aidl.IPayService;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //绑定服务
conn=new MyServiceConnection();
bindService(new Intent("cn.pay.service.PayService"), conn, Context.BIND_AUTO_CREATE);
} private MyServiceConnection conn = null;
private IPayService iPayService; private class MyServiceConnection implements ServiceConnection { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
iPayService = IPayService.Stub.asInterface(service);
System.out.println("绑定成功");
} @Override
public void onServiceDisconnected(ComponentName name) {
iPayService = null;
} } public void pay(View v) {
System.out.println("begin pay");
try {
//调用方法.
iPayService.pay("a", "b", "c", 100);
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("end pay");
} }