什麼是App Indexing?
將網站上的網頁和智能手機相關聯。已安裝了相應軟件的用戶可以通過網站直接打開應用內容。
詳細信息請參見官網https://developers.google.com/app-indexing/webmasters/app
官網上寫的非常詳細,可以看中文的。
代碼實現
HTML網頁中要提供App Indexing的網址是http://example.com/淘寶店鋪ID。淘寶店鋪ID是不確定的值。
在AndroidMenifest.xml中聲明Intent過濾器。
<activity android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_title_viewgizmos">
<action android:name="android.intent.action.VIEW" />
<!-- 可以獲取包含 "http://example.com/g23422" 開頭的url -->
<data android:scheme="http"
android:host="example.com"
android:pathPrefix=".*"
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
可以使用下面兩種方式測試是否能跳轉到淘寶店鋪ID爲【g23422】的app頁面:
adb shell am start -a android.intent.action.VIEW -d "http://example.com/g23422" com.example.android或者在HTML網頁上添加:
<a href="intent://example.com/gizmos#Intent;scheme=http;package=com.example.android;end;">
http://example.com/g23422
</a>
App Indexing的測試方法
在https://developers.google.com/app-indexing/webmasters/test中輸入【android-app://packageName/scheme/host/pathPrefix】。詳情請參考下圖:
如何判斷應用從App Indexing進入的?
方法如下:
public boolean isCallByAppIndexing(Activity activity) {
Intent intent = activity.getIntent();
Uri uri = intent.getData();
if (uri != null) {
String scheme = uri.getScheme();
String host = uri.getHost();
if (scheme != null && scheme.equals("http")) {
if (host != null && host.equals("example.com")) {
return true;
}
}
}
return false;
}
獲取淘寶店鋪ID的方法:
public String getShopId(Activity activity) {
String shopId = null;
Uri uri = activity.getIntent().getData();
if (uri != null) {
List<String> list = uri.getPathSegments();
shopId = (list == null ? null : list.get(0));
}
return shopId;
}