您的位置:首页 > 大数据 > 人工智能

Service (aidl远程服务)

2016-01-28 16:35 399 查看

Service (aidl远程服务)

game工程调用alpay工程的pay服务

alpay工程



支付的服务

public class PayService extends Service {

@Override
public IBinder onBind(Intent intent) {
AiPay aiPay = new AiPay();
return aiPay;
}

class AiPay extends Stub{
@Override
public void pay() throws RemoteException {
PayService.this.pay();
}
}

public void pay(){
System.out.println("检测支付环境是否安全");
System.out.println("加密账号密码");
System.out.println("建立连接");
System.out.println("上传数据");
System.out.println("完成支付");
}

}


aidl (Android Interface Definition Language)

去掉public声明

package com.example.alpay;

interface PayInterface {
void pay();
}


AndroidManifest.xml配置

<service android:name="com.example.alpay.PayService">
<intent-filter >
<action android:name="com.example.alpay"/>
</intent-filter>
</service>


game工程



aidl文件复制到同名包下



在game工程中调用alpay工程的pay方法

// 隐式启动服务
Intent intent = new Intent();
intent.setAction("com.example.alpay");
// 绑定服务, 内部类实现ServiceConnection接口
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
asInterface = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}, BIND_AUTO_CREATE);

findViewById(R.id.btn_pay).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
asInterface.pay();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: