H5網頁呼叫原生支付寶支付的完整demo
阿新 • • 發佈:2019-01-31
檢測到支付寶時會調起原生APP,檢測不到支付寶時會調起網頁版支付寶
package cn.showmac.simpledemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.KeyEvent; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; import com.alipay.sdk.app.PayTask; import com.alipay.sdk.util.H5PayResultModel; import java.lang.ref.WeakReference; public class H5Activity extends BaseActivity { private WebView mWebView; private ProgressBar bar; private String baseUrl = "http://5000.gr2c5266.showmac.ali-sh.goodrain.net:10080"; private TestHandler mHandler = new TestHandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_h5); bar = (ProgressBar) findViewById(R.id.progressBar); mWebView = (WebView) findViewById(R.id.webView); WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); settings.setAllowFileAccess(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setDomStorageEnabled(true); settings.setGeolocationEnabled(true); mWebView.loadUrl(baseUrl); mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); if (newProgress == 100) { bar.setVisibility(View.INVISIBLE); } else { bar.setVisibility(View.VISIBLE); bar.setProgress(newProgress); } } }); mWebView.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(final WebView view, String url) { final PayTask task = new PayTask(H5Activity.this); //處理訂單資訊 final String ex = task.fetchOrderInfoFromH5PayUrl(url); if (!TextUtils.isEmpty(ex)) { //呼叫支付介面進行支付 new Thread(new Runnable() { public void run() { H5PayResultModel result = task.h5Pay(ex, true); Message message = new Message(); message.what = 0; mHandler.sendMessage(message); } }).start(); } else { view.loadUrl(url); } return true; } } ); findViewById(R.id.back).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); //goBack()表示返回WebView的上一頁面 return true; } finish(); return false; } @Override protected void onDestroy() { super.onDestroy(); if (mWebView != null) { mWebView.removeAllViews(); mWebView.destroy(); } if (mHandler != null) { mHandler.removeCallbacksAndMessages(null); mHandler = null; } } public class TestHandler extends Handler { WeakReference<Activity> mActivity; public TestHandler(Activity activity) { mActivity = new WeakReference<>(activity); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); Activity activity = mActivity.get(); if (activity != null) { if (msg.what == 0) { mWebView.loadUrl(baseUrl); } } } } }