现在的位置: 首页 > 综合 > 正文

如何通过超链接打开Activity并传入参数

2013年08月30日 ⁄ 综合 ⁄ 共 2458字 ⁄ 字号 评论关闭
 
通常如果我们在程序中要打开一个url,一般是借助于浏览器来打开,但用户手机上会安装多个浏览器,Android做的比较智能,把选择权让给了用户,让用户选择用哪个浏览器来打开


程序代码通常如下,指明了action、category和data uri
             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
             intent.addCategory(Intent. CATEGORY_BROWSABLE);
             intent.addCategory(Intent. CATEGORY_DEFAULT);
             startActivity(intent);

浏览器应用要想处理这个请求动作的话,那么它的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:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>

上面是一些最基本的配置,说明了它能够处理VIEW action,BROWSABLE和DEFAULT类别,uri scheme为http和https的Intent,我们可以定义自己的uri scheme以便能处理除常规外一些特定的data uri scheme
            <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:scheme="http" />
                <data android:scheme="https" />
                <data android:scheme="myscheme" />
            </intent-filter>

代码中的Intent编写为
             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myscheme://www.baidu.com"));
             intent.addCategory(Intent. CATEGORY_BROWSABLE);
             intent.addCategory(Intent. CATEGORY_DEFAULT);
             startActivity(intent);

基于前面的一些介绍,我们知道在代码中我们肯定是可以通过我们自己的scheme来打开一个Activity的,那通过一个超链接是否也能做到呢,让人欣喜的是Android系统提供了这种功能

Activity <intent-filter>配置如下
             <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:scheme="my.special.scheme" />
            </intent-filter>

下面是一个html页面,通过uri(scheme为我们自定义的scheme:my.special.scheme)往Activity传入了两个参数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>default</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="viewport" content="width=320" />
</head>

<body>
<div id="header">
     <a href="my.special.scheme://192.168.2.126/param1/param2">link open app</a>
</div>
</body>
</html>

当点击超链接时,相应的Activity就能获取到data uri了
             Uri data = getIntent().getData();
             String scheme = data.getScheme();
             String host = data.getHost();
             List<String> params = data.getPathSegments();
             String first = params.get(0);
             String second = params.get(1);
             LogUtil. i("INFO", "param1 : " + first + "   param2 : " + second + "   scheme : " + scheme + "   host : " + host);

Activity中打印数据如下
05-10 16:00:27.090: I/INFO(2798): param1 : param1   param2 : param2   scheme : my.special.scheme   host : 192.168.2.126


抱歉!评论已关闭.