网页打开activity有好几种方法,不过现在我主要介绍的是使用网页打开activity。
首先我们都知道的是,网址的格式:content://host:port/path ,比如:http://10.205.1.10:8080/text.html .
在android中我们也可以使用scheme给activity设置这些东西,设置的方法为:
<activity
android:name="你的Activity地址"
android:exported="true"
android:label="@string/title_activity_intent" >
<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="10.205.1.10" //随你喜欢设置成什么,可以是任何类型的数据
android:scheme="content" />//和host一样
</intent-filter>
</activity>
代表scheme的网址格式是:
content://com.example.project:8080/folder/subfolder/etc
\---------/ \---------------------------/ \---/ \--------------------------/
scheme host port path
\--------------------------------/
authority
我们就可以直接获取Activity了,
下面是一个demo:
首先是网页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<a href="content://thisisatest:8080?test1=1&test2=2" >test</a>
</body>
</html>
然后我们的Activity设置:
<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="thisisatest"
android:port="8080"
android:scheme="content" />
</intent-filter>
这样我们就可以跳转了
获取数据的话 我们需要添加一下代码:
<span style="white-space:pre"> </span>Intent intent = getIntent();
String scheme = intent.getScheme();
Uri uri = intent.getData();
<span style="white-space:pre"> </span>String id = uri.getQueryParameter("test1");
<span style="white-space:pre"> </span>String returnurl = uri.getQueryParameter("test2") ;