Android 從網(wǎng)頁中跳轉(zhuǎn)到本地App

我們在使用微信、QQ、京東等app的時候,會發(fā)現(xiàn)有時候通過他們的wap網(wǎng)頁可以打開本地app,如果安裝了則直接跳轉(zhuǎn),沒有安裝的話直接跳轉(zhuǎn)應(yīng)用商店

網(wǎng)頁跳轉(zhuǎn)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,測試如何接收外部跳轉(zhuǎn):

在我們的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 接收外部跳轉(zhuǎn)過濾器-->
            <intent-filter>
                <!-- 協(xié)議部分配置 ,要在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里設(shè)置了 scheme和host,則該Activity可以接收和處理類似于 "test://splash"的Uri。

網(wǎng)頁端需要配置如下

<!DOCTYPE html>  
<html>  
<body>  
<iframe src="test://splash" style="display:none"></iframe>  
</body>  
</html>  

SO,當(dāng)我們從網(wǎng)頁跳轉(zhuǎn)的App的時候,如果本地安裝了,那么就可以順利跳轉(zhuǎn)過來了, 是不是感覺So easy 呢?

如果你想在單獨處理外部跳轉(zhuǎn)的Uri可以,在接收外部跳轉(zhuǎn)的Activity中添加如下代碼:

       Intent intent = getIntent();
        String data = intent.getDataString();
        if (data.equals("yijj://splash")){
            // TODO: 在這里處理你想干的事情。。。 
            startActivity(new Intent(this,EntranceActivity.class));
        }else {
            finish();
        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容