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之间的通信(调用)的更多相关文章

  1. Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  2. Fragment的生命周期和Activity之间的通信以及使用

    Fragment通俗来讲就是碎片,不能单独存在,意思就是说必须依附于Activity,一般来说有两种方式把Fragment加到Activity,分为静态,动态. 静态即为右键单击,建立一个Fragme ...

  3. Fragmen和Activity之间的通信--接口和实现的分离(转)

    Fragmen和Activity之间的通信--接口和实现的分离(转) 分类: Android平台     在平板的开发过程中通常都会采用多个Fragment的实现方式,通常有一个为list的Fragm ...

  4. 【Android 应用开发】Activity生命周期 与 Activity 之间的通信

    一. Activity生命周期 上图 1. Activity状态 激活状态 : Activity出于前台 , 栈顶位置; 暂停状态 : 失去了焦点 , 但是用户仍然可以看到 , 比如弹出一个对话框 , ...

  5. Activity生命周期 与 Activity 之间的通信

    一. Activity生命周期 上图 1. Activity状态 激活状态 : Activity出于前台 , 栈顶位置; 暂停状态 : 失去了焦点 , 但是用户仍然可以看到 , 比如弹出一个对话框 , ...

  6. 适配器(adapter)与fragment之间、fragment与activity之间的通信问题

    一.适配器(adapter)与fragment之间通信 通过本地广播进行通信 步骤如下 在adapter中代码 声明本地广播管理 private LocalBroadcastManager local ...

  7. Activity之间的通信

    通常Activity之间的通信有三种方式:单向不传参数通信.单项传参数通信和双向通信. 这几种传递方式都需要通信使者Intent.以下将用代码来辅助理解. 1.单向不传递参数通信 public cla ...

  8. Android中BroadCast与Activity之间的通信

    在看本文之前,假设你对于Android的广播机制不是非常了解.建议先行阅读我转载的一篇博文:图解 Android 广播机制. 因为本案例比較简单,故直接在此贴出代码,不做过多的阐述. 先上效果截图: ...

  9. Android中两个Activity之间简单通信

    在Android中,一个界面被称为一个activity,在两个界面之间通信,采用的是使用一个中间传话者(即Intent类)的模式,而不是直接通信. 下面演示如何实现两个activity之间的通信. 信 ...

随机推荐

  1. how to optimize javascript performance

    https://developers.google.com/speed/articles/optimizing-javascript http://developer.yahoo.com/perfor ...

  2. SKYLINE

    uvalive4108:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  3. cf D Bear and Floodlight

    题意:有n个灯,每个灯有一个照亮的角度,现在从点(l,0)走到点(r,0),问这个人若一直被灯照着能最多走多远? 思路:状压dp,然后通过向量旋转求出点(dp[i[,0)与灯的坐标(p[j].x,p[ ...

  4. WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞

    漏洞名称: WordPress Pie Register插件‘wp-login.php’多个跨站脚本漏洞 CNNVD编号: CNNVD-201307-255 发布时间: 2013-07-31 更新时间 ...

  5. 使用StaticResource给控件定义公共的样式和属性来写界面XAML

    一:效果图 二:定义公共的样式和属性 在MainPage.xaml中 <phone:PhoneApplicationPage.Resources> <SolidColorBrush ...

  6. Delphi NativeXml读取中文乱码问题解决

    NativeXml默认的字符类型为Utf8String,有时在读取中文时还是会出现乱码问题,在329版本中提供一种类型转换函数sdUtf8ToWide(),我们可以这样sdUtf8ToWide(AXm ...

  7. Remove Duplicates from Sorted Array II ——LeetCode

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. HDOJ(HDU) 2162 Add ‘em(求和)

    Problem Description Write a program to determine the summation of several sets of integers. Input Th ...

  9. 尚学堂 JAVA DAY12 java程序执行时内存的分配

  10. 借助github搭建自己的博客

    创建GitHub技术博客全攻略 通过GitHub Pages建立个人站点(详细步骤) 备注: 我搭建成功了,但是访问时只能使用"http://username.github.io/usern ...