我們?cè)谑褂梦⑿拧Q叠荠、京東等app的時(shí)候匿沛,會(huì)發(fā)現(xiàn)有時(shí)候通過他們的wap網(wǎng)頁可以打開本地app,如果安裝了則直接跳轉(zhuǎn)蝙叛,沒有安裝的話直接跳轉(zhuǎn)應(yīng)用商店
網(wǎng)頁跳轉(zhuǎn)app的原理如下:
對(duì)于Android平臺(tái)URI主要分三個(gè)部分:scheme, authority and path俺祠。其中authority又分為host和port。
格式如下:
scheme://host:port/path
舉個(gè)栗子:
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" />
下面是一個(gè)測(cè)試demo借帘,測(cè)試如何接收外部跳轉(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的時(shí)候腿准,如果本地安裝了际起,那么就可以順利跳轉(zhuǎn)過來了, 是不是感覺So easy 呢吐葱?
如果你想在單獨(dú)處理外部跳轉(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();
}