Android 從網頁中跳轉到本地App

gggggdf 7年前發布 | 15K 次閱讀 安卓開發 Android開發 移動開發

我們在使用微信、QQ、京東等app的時候,會發現有時候通過他們的wap網頁可以打開本地app,如果安裝了則直接跳轉,沒有安裝的話直接跳轉應用商店

網頁跳轉app的原理如下:

對于Android平臺URI主要分三個部分:scheme, authority and path。其中authority又分為host和port。

格式如下:

scheme://host:port/path

舉個栗子:

URI栗子

下面看下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.jianshu.com/p/356975d493bf

 

 本文由用戶 gggggdf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!