android app接入微信H5支付

android app接入微信h5支付的代码 

public class ThirdPay{
    private static ThirdPay     payPtr;
    private static AppActivity _activity;
    private String orderInfo;
    private static FrameLayout m_layout;
    public void init(AppActivity context){
        _activity=context;
        payPtr=this;
        m_layout=(FrameLayout)_activity.getWindow().getDecorView();
    }
	
    ///-------------weixin-------------
    public static void start_pay_wx(String extension){
        payPtr.orderInfo=extension;
        payPtr.wx_do_pay();
    }
	
    public void wx_do_pay(){
        _activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                WebView webView = new WebView(_activity);
                WebSettings settings = webView.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setDefaultTextEncodingName("UTF-8");
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.setWebChromeClient(new WebChromeClient());

                Map<String, String> extraHeaders = new HashMap<String, String>();
                extraHeaders.put("Referer", "xxxx");//xxxx为 商户申请H5时提交的授权域名
                webView.loadUrl(payPtr.orderInfo,extraHeaders);
                webView.setAlpha(0);
                FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
                
                m_layout.addView(webView, lParams);

                webView.setWebViewClient(new WebViewClient() {  
                @Override  
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.startsWith("weixin://")) {
                        try{
                            Intent intent = new Intent();                    
                            intent.setAction(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(url));
                            _activity.startActivity(intent);
                        }catch(Exception e){
                            Toast.makeText(_activity, "未安装微信应用", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    } else {
                        Map<String, String> extraHeaders = new HashMap<String, String>();
                        view.loadUrl(url, extraHeaders);
                    }
                    return super.shouldOverrideUrlLoading(view, url);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub              
                    super.onPageFinished(view, url);
                    if(m_layout!=null){
                        m_layout.removeView(view);
                    }
                    AppActivity.debugShowLog(url);
                }

                @Override
                public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                    // TODO Auto-generated method stub              
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
                });
            }
        });
    }
}

 

展开阅读全文

没有更多推荐了,返回首页