Android 通过URL scheme 启动App

转载于:http://www.jianshu.com/p/7d90a6cfb5f3

Android 通过URL scheme 启动App


简述:Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数。


Step 0:

关于页面内容格式如下:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>

各个项目含义如下所示:

  • scheme:判别启动的App。 - 必填项
  • host:适当记述。- 必填项
  • path:传值时必须的key。 - 非必填项
  • query:获取值的Key和Value。 - 非必填项

Step 1:

在Android端定义Url启动格式,在AndroidManifest.xml文件中,指定需要进行启动的Activity页面,一般是app启动的主页面。

示例:

 <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <!--通过浏览器Url启动app-->
            <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="lolita"
                        android:scheme="night" />
                <!--<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>-->
            </intent-filter>
        </activity>
    </application>

Step 2:

定义一个HTML文件 start.html

   <!DOCTYPE html>
    <html>

        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>

        <body>
            <br/>
            <!--<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>-->
            <a href="night://lolita?name=Tomcat&page=27">打开app</a><br/>

    </html>

Step 3:

如何获取url调整传递过来的数据?

在Activity中需要取值的地方添加以下代码:

Intent intent = getIntent();
if (intent != null)
{
String intentAction = intent.getAction();
if (Intent.ACTION_VIEW.equals(intentAction))
{
Uri intentData = intent.getData();
String name = intentData.getQueryParameter("name");
String page = intentData.getQueryParameter("page");
Log.e(TAG, "initIntentData: " + name);
Log.e(TAG, "initIntentData: " + page);

运行结果:


输出log

参考网址:

1:http://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app/3472228#3472228

2:http://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser



作者:beforenight
链接:http://www.jianshu.com/p/7d90a6cfb5f3
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值