private class MyBinder extends Binder implements ALiPayBinder {
@Override
public void callPay(String username, String password, float money) {
pay(username, password, money);
}
}
远程服务:
private class MyBinder extends ALiPayBinder.Stub
3、远程服务这边就处理完毕了,现在处理调用者这边
首先将远程服务下的ALiPayBinder.aidl完整拷贝过来,包括包名,然后可以发现在调用者这边gen也自动生成了ALiPayBinder.java
4、在调用者这边的 onServiceConnected()方法中,使用下面的获取远程服务返回的代理人,
ALiPayBinder aLiPayBinder = ALiPayBinder.Stub.asInterface(service);
5、调用者调用支付宝提供的远程支付服务
public void pay(View view) {
try {
aLiPayBinder.callPay("hacket", "123456", 1234.43f);
} catch (RemoteException e) {
e.printStackTrace();
}
}
1、远程服务(支付宝)
ALiPayService.java
package com.alipay.remotePayService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
/**
* 支付宝远程支付服务
*/
public class ALiPayService extends Service {
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}
/**
* TODO 2、代理对象
*/
private class MyBinder extends ALiPayBinder.Stub {
@Override
public void callPay(String username, String password, float money) {
pay(username, password, money);
}
}
/**
* TODO 1、 支付宝远程支付服务
* @param username 用户名
* @param password 密码
* @param money 支付金额
*/
public void pay(String username, String password, float money) {
System.out.println("支付宝支付[" + "\t用户名:" + username +"\t密码:"+ password +"\t支付金额:"+money+"]");
}
@Override
public void onCreate() {
System.out.println("支付宝远程支付服务创建了。。。");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("支付宝远程支付服务停止了。。。");
super.onDestroy();
}
}
ALiPayBinder.aidl
package com.alipay.remotePayService;
interface ALiPayBinder {
/**
* TODO 3、调用支付服务
*/
void callPay(String username, String password, float money);
}
AndroidManifest.xml
<!--TODO 4、注册支付宝远程服务 -->
<service android:name="com.alipay.remotePayService.ALiPayService">
<intent-filter >
<action android:name="com.alipay.remotePay"/>
</intent-filter>
</service>
2、调用者这边
MainActivity.java
package cn.zengfansheng.userALiPay;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import com.alipay.remotePayService.ALiPayBinder;
public class MainActivity extends Activity {
private ServiceConnection conn;
private ALiPayBinder aLiPayBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* TODO 5、绑定到远程支付宝服务
* @param view
*/
public void bindRemote(View view) {
Intent remoteService = new Intent();
remoteService.setAction("com.alipay.remotePay");
conn = new MyConn();
this.bindService(remoteService, conn, Context.BIND_AUTO_CREATE);
}
/**
* TODO 六、远程支付连接反馈
*/
private class MyConn implements ServiceConnection{
// TODO 七、连接成功后,调用,参数二、为代理对象
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
aLiPayBinder = ALiPayBinder.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
/**
* TODO 八、调用支付宝远程支付服务
* @param view
*/
public void pay(View view) {
try {
aLiPayBinder.callPay("hacket", "123456", 1234.43f);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
ALiPayBinder.aidl
package com.alipay.remotePayService;
interface ALiPayBinder {
/**
* TODO 3、调用支付服务
*/
void callPay(String username, String password, float money);}
1、先开启远程服务应用
2、再开启本地应用