1,首先配置 Manifest

	// 根据意图来进行显示跳转
  <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


            <intent-filter>
                <data android:scheme="test"  android:host="你的域名或地址(不重要)"/>

                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>

传递参数,打开指定的页面。

2.HTML的跳转链接里面添加参数


<a href=" scheme的内容 :// host的内容?传递参数的key=传递参数的value">随意什么内容...</a> 

<!DOCTYPE html>
<html>
<head>
    <title>Android短信测试</title>
</head>
<body>
    <a href="test://bao.cn">启动红色程序</a>
</body>
</html>

scheme:启动的App的标识,相当于协议吧。

host:域名,不重要。

query:传给app参数的Key和Value 。

3.Android代码,在第一启动页加入下面代码

  public static final String TYPE_INTENT = "type";
    public static final String URL_INTENT = "url";
    public static final String NAME_INTENT = "name";

    if (intent.getData() != null)
        {
            Uri uri = intent.getData();
            uri.getScheme();//获取scheme
            uri.getHost();//获取host
            uri.getAuthority();//获取authority
            String type = uri.getQueryParameter(TYPE_INTENT);//获取传递参数
            String url = uri.getQueryParameter(URL_INTENT);
            String name = uri.getQueryParameter(NAME_INTENT);
            //标题转UTF-8码
            if (!TextUtils.isEmpty(name))
            {
                try
                {
                    name = URLDecoder.decode(name, "UTF-8");
                } catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }
            }
        }
		参数可以传空的,如果是中文要转码,断点看看参数
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐