项目中,需要在App中使用webview通过访问url,最终调起微信支付。
mWebView = (WebView) findViewById(R.id.wv_action); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); mWebView.addJavascriptInterface(new ExpJavaScriptInterface(), "android"); mWebView.loadUrl(webUrl); mWebView.setWebChromeClient(wcc); // findViewById(R.id.ll_descovery_action_back).setOnClickListener(this); mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { LogUtils.d("url", " shouldOverrideUrlLoading == " + url); // 如下方案可在非微信内部WebView的H5页面中调出微信支付 if (url.startsWith("weixin://wap/pay?")) { try{ Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); startActivity(intent); }catch (ActivityNotFoundException e){ MiscUtil.toastShortShow(mContext, "请安装微信最新版!"); } }else{ view.loadUrl(url); } return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { findViewById(R.id.pb_action_progress).setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { findViewById(R.id.pb_action_progress).setVisibility(View.GONE); update(); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { view.stopLoading(); view.clearView(); view.loadUrl("about:blank"); view.clearHistory(); update(); } }); } final class ExpJavaScriptInterface{ ExpJavaScriptInterface() { } @JavascriptInterface public void OpenOtherApp(String ss){ } @JavascriptInterface public void OpenOtherActivity(String flag, String gotoUrl, String title){ GoToManager.toTask4Flag(ActionActivity.this, flag, gotoUrl, title); ActionActivity.this.finish(); } @JavascriptInterface public void Browser(String apkurl,String apkName){ } @JavascriptInterface public void RefreshWeb(){ } }基于上述的实现,是因为安卓App可以使用intent来调用URI数据,从而实现该app不能完成的功能。例如打开网页,跳转到电话拨打页面,或者是跳转到微信支付。
就Android平台而言,URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。格式如下: scheme://host:port/path
如果要实现在一个App打开另一个App 的页面 如 BActivity,该如何做呢
在BActivity manifest.xml 配置如下
<intent-filter> <data android:scheme="myapp"/> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> </intent-filter> 跳转如下,配置成你需要的Uri。Uri中可传递一些参数,在目标页面进行解析。