Android 从网页中跳转到本地App

我们在使用微信、QQ、京东等app的时候,会发现有时候通过他们的wap网页可以打开本地app,如果安装了则直接跳转,没有安装的话直接跳转应用商店

网页跳转app的原理如下:

对于Android平台URI主要分三个部分:scheme, authority and path。其中authority又分为host和port。
格式如下:

scheme://host:port/path

举个栗子:

下面看下data flag
<data android:host="string" 
      android:mimeType="string" 
      android:path="string" 
      android:pathPattern="string" 
      android:pathPrefix="string" 
      android:port="string" 
      android:scheme="string" />

下面是一个测试demo,测试如何接收外部跳转:

在我们的App入口Activity的清单文件中配置如下:
<activity
            android:name=".EntranceActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@style/Entrance">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

            <!--Android 接收外部跳转过滤器-->
            <intent-filter>
                <!-- 协议部分配置 ,要在web配置相同的-->
                <data
                    android:host="splash"
                    android:scheme="test"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

                <action android:name="android.intent.action.VIEW"/>
            </intent-filter>

        </activity>

如上所示,在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "test://splash"的Uri。
网页端需要配置如下:
<!DOCTYPE html>  
<html>  
<body>  
<iframe src="test://splash" style="display:none"></iframe>  
</body>  
</html>

SO,当我们从网页跳转的App的时候,如果本地安装了,那么就可以顺利跳转过来了, 是不是感觉So easy 呢?

如果你想在单独处理外部跳转的Uri可以,在接收外部跳转的Activity中添加如下代码:

Intent intent = getIntent();
String data = intent.getDataString();
if (data.equals("yijj://splash")){
     // TODO: 在这里处理你想干的事情。。。 
     startActivity(new Intent(this,EntranceActivity.class));
}else {
     finish();
}


http://www.open-open.com/lib/view/open1484051365201.html
  • 大小: 34.3 KB
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值