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

猜你喜欢

转载自gundumw100.iteye.com/blog/2352370