H5与Activity之间的通信(调用)
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>