码迷,mamicode.com
首页 > 其他好文 > 详细

H5与Activity之间的通信(调用)

时间:2015-08-31 19:36:48      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

1.通过H5页面的超链接尾部信息不同进行跳转(类似于URL拦截器)

例如:控件x的href="http://www.example.com/?menu_id=1",

在代码中添加webview

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/**
 * Author:JsonLu
 * DateTime:2015/8/31 17:52
 * Email:luxd@i_link.cc
 */
public class SecoundActivity extends Activity{

    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView)findViewById(R.id.webview);
        String url = "http://jcut.sinaapp.com/androidjs/second.html?menu_id=1";
        this.mWebView.loadUrl(url);
        mWebView.setWebViewClient(new MyWebViewClient());

    }
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("URL",url);
            Intent it = new Intent();
            if(url.endsWith("?menu_id=1")){
                it.setClass(SecoundActivity.this, MainActivity.class);
                startActivity(it);
            }else{
                Toast.makeText(SecoundActivity.this,"时间:"+System.currentTimeMillis(),Toast
                        .LENGTH_LONG).show();
            }
            return true;
        }
    }
}

2.通过js调用本地类的方法

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

/**
 * Author:JsonLu
 * DateTime:2015/8/31 17:52
 * Email:luxd@i_link.cc
 */
public class MainActivity extends Activity {


    private WebView mWebView;

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

    @TargetApi(Build.VERSION_CODES.ECLAIR_MR1)
    private void initView() {
        this.mWebView = (WebView) this.findViewById(R.id.webview);
        //可以执行javascript
        this.mWebView.getSettings().setJavaScriptEnabled(true);
        String url = "http://jcut.sinaapp.com/androidjs/";
        this.mWebView.loadUrl(url);
        //android添加javascript代码,让H5页面能够调用,第二个参数对应的是H5的
        this.mWebView.addJavascriptInterface(new PayJavaScriptInterface(), "js");
    }

    private class PayJavaScriptInterface {

        @JavascriptInterface
        public void jumpActivity(int param) {
            if (param == 1) {
                Intent it = new Intent();
                it.setClass(MainActivity.this, SecoundActivity.class);
                startActivity(it);
            } else {
                Toast.makeText(MainActivity.this, "时间:" + System.currentTimeMillis(), Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

index.html

<html>
<head>
<meta charset="UTF-8">
<title>交互Demo</title>
<meta content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" name="viewport">
<style>
body{width:100%;height:auto;text-align:center;background-color:#eee}
button{width:80%;height:40px;background-color:#36A803;border:1px solid #ddd;border-radius:5px;font-size:18px;color:#fff;margin-top:50px;}
</style>
</head>
<body>
<div id="userinfo"></div>
<button onClick="toActivity(1);">跳转</button>
<button onClick="toActivity(2);">显示时间</button>
<script>
function toActivity(param){
    //调用本地的类(js)的jumpActivity()方法;
    window.js.jumpActivity(param);
}
</script>
</body>
</html>

second.html

<html>
<head>
<meta charset="UTF-8">
<title>交互Demo</title>
<meta content="initial-scale=1.0,user-scalable=no,maximum-scale=1,width=device-width" name="viewport">
<style>
body{width:100%;height:auto;text-align:center;background-color:#eee;}
#userinfo{margin-top:50px}
a{text-decoration:none;width:80%;padding:5% 30% 5% 30%;height:40px;background-color:#36A803;border:1px solid #ddd;border-radius:5px;font-size:18px;color:#fff;}
</style>
</head>
<body>
<div style="margin-top:50px">
<a href="http://jcut.sinaapp.com/androidjs/index.html?menu_id=1">跳转</a>
</div>
<div style="margin-top:200px">
<a href="http://jcut.sinaapp.com/androidjs/index.html?menu_id=2">显示时间</a>
</div>
</body>
<html>

H5与Activity之间的通信(调用)

标签:

原文地址:http://www.cnblogs.com/Jsonlu/p/4773761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!