您的位置:首页 > 移动开发

Webview里JS跳转到Activity页面

2016-09-19 11:50 393 查看
有两种方法.

第一种是通过webview提供的js和java沟通的接口进行

public void addJavascriptInterface(Object object, String name)


一、然后,在javascript里可以通过name来引用到object对象里有@JavascriptInterface注解的方法

Java代码

WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView)findViewById(R.id.webView);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(this, "nativeMethod");
wv.loadUrl("file:///android_asset/index.html");
}

@JavascriptInterface
public void toActivity(String activityName) {
//此处应该定义常量对应,同时提供给web页面编写者
if(TextUtils.equals(activityName, "a")){
startActivity(new Intent(this,AActivity.class));
}else{
startActivity(new Intent(this,BActivity.class));
}
}


javascript代码

<!DOCTYPE HTML>
<html>

<script type="text/javascript">
function gotoActivity (activity) {
nativeMethod.toActivity(activity);
}
</script>
<body>

<button onClick="gotoActivity('a')">gotoActivity A</button>
<button onClick="gotoActivity('b')">gotoActivity B</button>

</body>
</html>


以上是第一种方法,在index.html页面可以跳转到AActivity和BActivity。

二、第二种方式里是利用系统提供的Activity隐式启动。

<!DOCTYPE HTML>
<html>

<body>
<a href="myapp://tonative/param?id=123">gotoActivity B</a>
<a href="http://www.baidu.com">open http link</a>
<a href="file:///android_asset/b.html">open local file</a>

</body>
</html>


当我们在webview里加载上面的代码,并点击gotoActivity B后,系统会去寻找能处理uri为”myapp://tonative/param?id=123”的Activity,默认情况下当然是没有的,所以我们可以把要打开的Activity按照约定的uri协议进行声明

<activity android:name=".BActivity"
android:parentActivityName=".AActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="tonative"
android:scheme="myapp" />
</intent-filter>
</activity>


我们可以通过声明不同的host实现打开不同的Activity,在打开的Activity里可以通过如下代码获取html页面传过来的参数

Intent intent = getIntent();
String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = intent.getData();
if(uri != null){
String id = uri.getQueryParameter("id");
Toast.makeText(this,id,Toast.LENGTH_LONG).show();
}
}


但这样其实有个问题,我们一般会在自己的WebviewActivity里给

wv.setWebViewClient(new WebViewClient(){})


从而实现在本页内的跳转都是由本Webview打开,而不是跳转到系统浏览器处理。这样设置后,‘href=”myapp://tonative/param?id=123”’这样的请求也被拦截到了本Webview里,从而失效,因此,我们需要做一个判断

wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

String scheme = Uri.parse(url).getScheme();//还需要判断host
if (TextUtils.equals("myapp", scheme)) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
});


return true,表明这次请求交给系统来处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息