Androidx学习笔记(71)--- 需要支付的应用(调用远程的服务)

来源:互联网 发布:2017下半年软件设计师 编辑:程序博客网 时间:2024/05/18 00:01

需要支付的应用

  1. 把刚才定义好的aidl文件拷贝过来,注意aidl文件所在的包名必须跟原包名一致
  2. 远程绑定支付宝的服务,通过onServiceConnected方法我们可以拿到中间人对象
  3. 把中间人对象通过Stub.asInterface方法强转成定义了pay方法的接口
  4. 调用中间人的pay方法
PayInterface.aidl
interface PayInterface {
 
void pay();
}

PayService.java
public class PayService extends Service {
 
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new PayPangZhi();
}
 
//中间人对象
class PayPangZhi extends Stub{
 
@Override
public void pay() throws RemoteException {
// 调用服务的pay方法
PayService.this.pay();
}
}
public void pay(){
System.out.println("检测运行环境");
System.out.println("加密用户名密码");
System.out.println("建立连接");
System.out.println("上传数据");
System.out.println("完成支付");
}
}

AndroidManifest.xml
<service android:name="com.exp.alipay.PayService">
<intent-filter >
<action android:name="com.exp.pangzhi"/>
</intent-filter>
</service>
0 0
原创粉丝点击