可由第三方應用喚醒,也可以由網頁喚醒,也可以通過adb命令直接測試喚醒。
1.網頁喚起用例:
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">
</head>
<html>
<input type="button" value="點擊我打開Deeplink" onclick="javascrtpt:window.location.href='test://xiaoshuo.com/detail?id=28493'">
</html>
直接保存爲html文件,存入手機中,然後選擇用瀏覽器打開,點擊“點擊我打開Deeplink”按鈕後,會顯示下面的彈框,點擊確認,即可喚醒相關應用。
2.adb命令喚起
adb shell am start -a android.intent.action.VIEW -d "test://xiaoshuo.com/detail?id=28493"
3.應用喚起
4.被喚醒應用設置
4.1AndroidManifest設置
<activity android:name="com.xiaoshuo.activity.DetailActivity" android:theme="@style/TranslucentTheme" >
<intent-filter>
<data
android:scheme="test"
android:host="xiaoshuo.com"
android:pathPrefix="/detail"
/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
4.2 Activity接收參數
// 判斷是否外部喚起
Uri data = getIntent().getData();
if (data != null) {
String host = data.getHost();
String path = data.getPath();
String id = data.getQueryParameter("id");
String scheme = data.getScheme();
Logger.i(TAG, "host: " + host);// xiaoshuo.com
Logger.i(TAG, "path: " + path);// detail
Logger.i(TAG, "scheme: " + scheme);// test
Logger.i(TAG, "id: " + id);// 10086
}
這個時候就可以測試DeepLink喚醒了。
至於是否直接進入到對應的Activity,還是需要判斷被喚醒Activity之前的界面是否存在,然後再一級一級的進行跳轉,就需要具體問題具體分析了。不過我們的應用沒有考慮那麼複雜,只需要喚醒進入對應的詳情界面即可,按返回鍵時,會判斷上一個Activity的界面是否存在,不存在則重新打開即可。