JS中调android的方法

android 与JS之间的交互

我们在做项目的时候总会遇到这样那样的业务需求,最近就遇到了要在Js中调用android的业务,在做项目开发之前写了一个demo。

网上很多的demo都是在Android的项目中穿点assets文件,把页面放在其中来调用,其实和获取网上的地址是一样的使用方式。

直接贴上代码:
toSucess.html

<!DOCTYPE html>
<html>
  <head>
    <title>toSucess.html</title>
	<meta charset="utf-8">
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
	<style type="text/css">
			.file {
			    position: relative;
			    display: inline-block;
			    background: #D0EEFF;
			    border: 1px solid #99D3F5;
			    border-radius: 4px;
			    padding: 4px 12px;
			    overflow: hidden;
			    color: #1E88C7;
			    text-decoration: none;
			    text-indent: 0;
			    line-height: 20px;
			}
		.file input {
		    position: absolute;
		    font-size: 100px;
		    right: 0;
		    top: 0;
		    opacity: 0;
		}
		.file:hover {
		    background: #AADFFD;
		    border-color: #78C3F3;
		    color: #004974;
		    text-decoration: none;
		}
		
	</style>
  </head>
  
  <body>
  	<div style="padding-top: 50px;padding-left: 50%">
        <!--特别注意的是在onclick事件的时候,window.android.finishClose()如上:一定要在方法名前面加上window.android.-->
	  <a href="javascript:void(0);" onclick="window.android.finishClose()" class="file">支付成功
		</a>
    </div>
  </body>
</html>

MainActivity.class

public class MainActivity extends AppCompatActivity {
    private ProgressWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        webView = (ProgressWebView) findViewById(R.id.webView);
        WebViewSettingUtil.setWebViewSetting(webView, MainActivity.this);
        webView.loadUrl("http://192.168.1.231:8088/water_exchange/waterexchange_jy/toSucess.html");
        webView.addJavascriptInterface(MainActivity.this, "android");
    }

    /**
     * js中调用的android方法
     */
    @JavascriptInterface
    public void finishClose() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, "finishClose", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

不要忘记加权限哦!!!

<uses-permission android:name="android.permission.INTERNET"/>

WebViewSettingUtil对WebView进行简单的包裹(这里是我做支付的时候用到的demo所以写的多了一点,正常是没有这么多的!!!)

public class WebViewSettingUtil {
    public static void setWebViewSetting(final WebView webView, final Activity activity) {
        if (webView != null) {
            WebSettings webSettings = webView.getSettings();
            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存
            webSettings.setJavaScriptEnabled(true);
            webSettings.setSupportZoom(false);
            webSettings.setBuiltInZoomControls(false);
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    System.out.println("info: 20171113 url ="+ url );
                    // 获取上下文, H5PayDemoActivity为当前页面
                    final Activity context = activity;
                    // ------  对alipays:相关的scheme处理 -------
                    if (url.startsWith("alipays:") || url.startsWith("alipay")) {
                        try {
                            context.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
                        } catch (Exception e) {
                            new AlertDialog.Builder(context)
                                    .setMessage("未检测到支付宝客户端,请安装后重试。")
                                    .setPositiveButton("立即安装", new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            Uri alipayUrl = Uri.parse("https://d.alipay.com");
                                            context.startActivity(new Intent("android.intent.action.VIEW",
                                                    alipayUrl));
                                        }
                                    }).setNegativeButton("取消", null).show();
                        }
                        return true;
                    }
                    // ------- 处理结束 -------
                    if (!(url.startsWith("http") || url.startsWith("https"))) {
                        return true;
                    }

                    view.loadUrl(url);
                    System.out.println("info:url = " + url);
                    return true;
                }
            });
        }
    }
}

随心笔记,不喜勿喷!!!

更多文章资源,欢迎关注:程序猿阵线联盟

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值