看板 KnucklesNote
作者 標題 [AndroidStudio] 建立應用程式索引 讓Google可搜尋APP內容
時間 2016-02-28 Sun. 21:30:49
參考 https://developers.google.com/app-indexing/
![[圖]](http://i.imgur.com/2IR3EYt.png)
在 Google Play Developer Console
點「服務和API」
在「將應用程式編入 GOOGLE 搜尋索引」,點「驗證網站」
![[圖]](http://i.imgur.com/Mb2RBVo.png)
輸入自己的網站位址後,點「驗證」
![[圖]](http://i.imgur.com/ToIeehI.png)
使用有該網站管理權限的 Google 帳戶登入 Google Search Console
點「核準或拒絕」,將App加入協作夥伴帳戶
![[圖]](http://i.imgur.com/kwvahcH.png)
在 Search Console 加入 App 為要管理的資源
要使用與 Google Play Developer Console 相同的 Google 帳戶登入 Search Console
在首頁點「新增內容」
![[圖]](http://i.imgur.com/DNZ2faT.png)
輸入android-app://{App Package Name}
![[圖]](http://i.imgur.com/kcBcBuD.png)
新增好後可以在設定的「使用者和資源擁有者」加入網站管理者的帳號
這樣就可以用同一個帳號管理網站和App了
接著修改程式
例如我們想要Google搜尋到這篇文章 http://disp.cc/b/11-9gMU 時
可以打開 App 的 TextActivity
並用Intent取得網址上的文章ID 11-9gMU
修改 AndroidManifest.xml
在起始頁 MainActivity 的設定裡,在原本就有的
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
後面再加上
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://disp.cc/m/main" -->
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="disp.cc" />
<data android:path="/m/" />
<data android:path="/m/main" />
</intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://disp.cc/m/main" -->
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="disp.cc" />
<data android:path="/m/" />
<data android:path="/m/main" />
</intent-filter>
在 TextActivity 的設定裡加上 <intent-filter>
<intent-filter android:label="@string/app_name">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://disp.cc/m/text/xx-xxx" -->
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="disp.cc" />
<data android:pathPattern="/m/text/..*-..*" />
</intent-filter>
pathPattern 只有正規式的 . 和 * 可以用 <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://disp.cc/m/text/xx-xxx" -->
<data android:scheme="http" />
<data android:scheme="https" />
<data android:host="disp.cc" />
<data android:pathPattern="/m/text/..*-..*" />
</intent-filter>
使用 ..* 就是任意字元要一個以上的意思
這邊使用的 http://disp.cc/m/text/xx-xxx 不用是真的網址
可以在網頁的 <head> 裡加上
<link rel="alternate" href="android-app://com.disp_tech.dispbbs/http/disp.cc/m/text/xx-xxx" />
讓搜尋引擎知道可以用什麼 App 打開,要用什麼 URI 來進入修改 TextActivity.java
將原本取得文章ID的程式
// 從其他Activity傳來的Intent
Bundle args = getIntent().getExtras();
mPageId = args.getInt("bi") + "-" + args.getString("ti");
改成
Intent intent = getIntent();
String action = intent.getAction();
String data = intent.getDataString();
if (Intent.ACTION_VIEW.equals(action) && data != null) {
// 使用 deep link 傳來的 Intent
mPageId = data.substring(data.lastIndexOf("/") + 1);
}else{
// 從其他 Activity 傳來的 Intent
Bundle args = intent.getExtras();
mPageId = args.getInt("bi") + "-" + args.getString("ti");
}
String action = intent.getAction();
String data = intent.getDataString();
if (Intent.ACTION_VIEW.equals(action) && data != null) {
// 使用 deep link 傳來的 Intent
mPageId = data.substring(data.lastIndexOf("/") + 1);
}else{
// 從其他 Activity 傳來的 Intent
Bundle args = intent.getExtras();
mPageId = args.getInt("bi") + "-" + args.getString("ti");
}
測試 deep link 是否有效
打開 AndroidStudio 下面的 Terminal 視窗
![[圖]](http://i.imgur.com/wFyBMcg.png)
先切換至 platform-tools 的目錄
例如作業系統是 Windows 7 的話輸入
cd %USERPROFILE%\AppData\Local\Android\sdk\platform-tools
(或是把這個目錄加進環境變數path裡,之後就不用再切換目錄)執行模擬器後,在 Terminal 執行
adb shell am start -a android.intent.action.VIEW -d "http://disp.cc/m/text/11-9gMU" com.disp_tech.dispbbs
看看 App 有沒有跳至閱讀文章的畫面執行
adb shell am start -a android.intent.action.VIEW -d "http://disp.cc/m/main" com.disp_tech.dispbbs
看看 App 有沒有跳至起始頁□ 錯誤解決記錄
在 Terminal 執行 adb 指令時,出現 error: more than one device/emulator
→ 關掉模擬器,在 Terminal 執行
adb kill-server
重新執行模擬器後再試試加入 App Indexing API
使用 App Indexing API 提供頁面的URL、標題、摘要等資訊
讓 Google 的搜尋引擎更容易製作 App 的索引
在 AndroidManifest.xml 的 <application> 裡加上
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
在 build.gradle (Module: app) 的 dependencies { 裡加上
compile 'com.google.android.gms:play-services-appindexing:9.2.1'
點 Sync Now修改 MainActivity.java
加上成員變數
private GoogleApiClient mClient;
private Uri mUrl;
private String mTitle;
private String mDesc;
private Uri mUrl;
private String mTitle;
private String mDesc;
在 onCreate() 裡加上要提供給搜尋引擎的資訊
mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
mUrl = Uri.parse("android-app://com.disp_tech.dispbbs/http/disp.cc/m/main");
mTitle = "熱門文章 - Disp BBS";
mDesc = "Disp BBS 是一個用網頁介面來模擬傳統BBS操作的討論區網站";
mUrl = Uri.parse("android-app://com.disp_tech.dispbbs/http/disp.cc/m/main");
mTitle = "熱門文章 - Disp BBS";
mDesc = "Disp BBS 是一個用網頁介面來模擬傳統BBS操作的討論區網站";
新增成員函式 getAction()
public Action getAction() {
Thing object = new Thing.Builder()
.setName(mTitle)
.setDescription(mDesc)
.setUrl(mUrl)
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
Thing object = new Thing.Builder()
.setName(mTitle)
.setDescription(mDesc)
.setUrl(mUrl)
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
在頁面開始與停止時,開啟與結束 AppIndexApi
@Override
public void onStart() {
super.onStart();
mClient.connect();
AppIndex.AppIndexApi.start(mClient, getAction());
}
@Override
public void onStop() {
AppIndex.AppIndexApi.end(mClient, getAction());
mClient.disconnect();
super.onStop();
}
public void onStart() {
super.onStart();
mClient.connect();
AppIndex.AppIndexApi.start(mClient, getAction());
}
@Override
public void onStop() {
AppIndex.AppIndexApi.end(mClient, getAction());
mClient.disconnect();
super.onStop();
}
依相同的步驟修改閱讀文章的 TextActivity 頁
測試搜尋結果
先在 Terminal 用 deep link 開啟一篇文章
adb shell am start -a android.intent.action.VIEW -d "http://disp.cc/m/text/11-9gMU" com.disp_tech.dispbbs
按Home回主畫面,使用 Google App 搜尋手機上的 App 內容
先在 Google App 的 設定/手機搜尋,看看自己的App有沒有被列上去並打勾
![[圖]](http://i.imgur.com/9N7x2eh.png)
搜尋 disp 看看
![[圖]](http://i.imgur.com/L6FcZDU.png)
測試點擊後是否能開啟 App 並跳至該篇文章
測試 Google 是否能存取應用程式頁面
在 Search Console 的 Google 模擬器
URI輸入 /http/disp.cc/m/main 或 /http/disp.cc/m/text/11-9gMU
若App已上架可選擇 Google Play APK,也可上傳還未上架的 APK
![[圖]](http://i.imgur.com/k4FfQzO.png)
測試的結果會像這樣
![[圖]](http://i.imgur.com/5m2Ydq8.png)
將 App 上傳至 Google Play 後
等個幾天後看 Search Console 的檢索狀態有沒有建立索引了
我是等了一週後才開始建立索引,之前都是會出現找不到APK的錯誤訊息
![[圖]](http://i.imgur.com/UqBci44.png)
--
※ 作者: Knuckles 時間: 2016-02-28 21:30:49
※ 編輯: Knuckles 時間: 2016-07-23 06:05:09
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 1435
回列表(←)
分享