1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| import android.app.Activity; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.annotation.NonNull; import android.util.Log;
import com.alipay.sdk.app.PayTask;
import java.util.Map;
public class AliPay {
private final Activity activity;
private final static int SDK_PAY_FLAG = 1;
private static final int SDK_AUTH_FLAG = 2;
private final Handler mHandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); switch (msg.what) { case SDK_PAY_FLAG: {
@SuppressWarnings("unchecked") PayResult payResult = new PayResult((Map<String, String>) msg.obj);
processPayResult(payResult); break; } case SDK_AUTH_FLAG: { break; } default: Log.w("AliPay", "收到未定义的消息标志"); Log.w("AliPay", msg.toString()); } } };
public AliPay(Activity activity) { this.activity = activity; }
public void Pay(String orderInfo) {
final Runnable payRunnable = new Runnable() {
@Override public void run() { PayTask alipay = new PayTask(activity); Map<String, String> result = alipay.payV2(orderInfo, true); Log.i("AliPay", result.toString());
Message msg = new Message(); msg.what = SDK_PAY_FLAG; msg.obj = result; mHandler.sendMessage(msg); } }; Thread payThread = new Thread(payRunnable); payThread.start(); }
private void processPayResult(PayResult payResult) { sendToScript("pay",payResult.toString()); }
private void sendToScript(String arg0, String arg1) { Log.d("AliPay",arg0); Log.d("AliPay",arg1);
} }
|