Android中使用AIDL调用远程服务

AIDL,Android Interface Definition Language,安卓接口定义语言。
IPC,Inter Process Communication,进程间通讯。

访问远程服务里的方法,需要用到AIDL。
绑定远程服务调用方法的步骤:
1、创建一个Service,里面有一个要被调用的方法。
2、创建一个AIDL文件IService,里面是一个接口,里面定义一个方法,用来调用Service中方法。保存后会在gen目录下生成一个包,在本地app中新建一个同名的包,将该AIDL文件复制到自己创建的包中。
3、在Service中定义一个MyBinder对象,继承IService.Stub,里面实现IService中的调用方法。然后再Service的onBind方法中,返回一个MyBinder实例。
4、在自己app的Activity中通过bindService方法绑定服务。
5、定义MyConn连接类–private class MyConn implements ServiceConnection,在里面的onServiceConnected方法中调用ISafePay.Stub.asInterface(service)获得MyBinder对象
6、使用该对象,调用IService接口中的方法。

下面的案例模仿游戏中的购买,远程服务为购买服务。

远程服务SafePayService.java

public class SafePayService extends Service {

    @Override
    public void onCreate() {
        System.out.println("支付宝服务被创建,一直在后台运行,检查手机的安全状态");
        super.onCreate();
    }

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("服务被绑定");
        return new MyBinder();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("服务onstart");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("服务解绑");
        return super.onUnbind(intent);
    }   

    @Override
    public void onDestroy() {
        System.out.println("支付宝服务被销毁");
        super.onDestroy();
    }

    /**
     * 安全支付的方法,要被调用
     * @param time
     * @param pwd
     * @param money
     */
    private boolean pay(long time, String pwd, double money) {
        if("123".equals(pwd))
            return true;
        else
            return false;
    }

    /**
     * 中介类
     * @author dong
     *
     */
    private class MyBinder extends ISafePay.Stub {
        @Override
        public boolean callPay(long time, String pwd, double money)
                throws RemoteException {
            return pay(time, pwd, money);
        }
    }

}

本地项目中的MainActivity.java

public class MainActivity extends Activity {
    private ISafePay iSafePay;
    private MyConn conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onDestroy() {
        Intent intent = new Intent();
        intent.setAction("com.example.alipay");
        stopService(intent);
        super.onDestroy();
    }

    public void click(View v) {
        Intent intent = new Intent();
        intent.setAction("com.example.alipay");
        conn = new MyConn();
        bindService(intent, conn, BIND_AUTO_CREATE); //异步操作

    }

    private class MyConn implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iSafePay = ISafePay.Stub.asInterface(service);
            try {
                boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f);
                if(result) {
                    Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "支付失败", Toast.LENGTH_SHORT).show();
                }
                unbindService(conn); 
                conn = null;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值