如果您當前使用的Firebase App Indexing API版本早於10.0.0,並且想要移至最新版本,請按照以下步驟操作。
在開始之前,請確保已將Firebase添加到您的應用程序。
更新應用程序索引庫
轉到應用程序的build.gradle腳本並添加以下依賴項:
dependencies { ... implementation 'com.google.firebase:firebase-appindexing:19.1.0' ... }
更新活動中的導入
// Beforeimport com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient;// After import com.google.firebase.appindexing.Action; import com.google.firebase.appindexing.FirebaseUserActions; import com.google.firebase.appindexing.Indexable; import com.google.firebase.appindexing.builders.Actions;
刪除對Google API客戶端的調用
public class ViewMessageActivity extends Activity { ... // Delete thisprivate GoogleApiClient mClient;... @Override protected void onCreate(Bundle savedInstanceState) { ... // Delete thismClient = new GoogleApiClient.Builder(this) .addApi(AppIndex.API) .build();... } }
更新用戶操作構建器
//Beforepublic Action getAction() { Thing object = new Thing.Builder() .setName(mText) .setUrl(mUrl) .build(); return new Action.Builder(Action.TYPE_VIEW) .setObject(object) .build(); }// After public Action getAction() { return Actions.newView(mText, mUrl); }
更新用戶操作調用
在使用新版本的API在應用程序中的任何內容上記錄操作之前,請確保已將其添加到設備上的索引中。現在,在對start()或stop()日誌的任何調用之前,都應先調用將內容添加到索引(如果尚未添加)的調用。
//Before@Override protected void onStart() { super.onStart(); mClient.connect(); AppIndex.AppIndexApi.start(mClient, getAction()); } @Override protected void onStop() { AppIndex.AppIndexApi.end(mClient, getAction()); mClient.disconnect(); super.onStop(); }// After @Override protected void onStart() { super.onStart(); // If you’re logging an action on an item that has already been added to the index, // you don’t have to add the following update line. FirebaseAppIndex.getInstance().update(getIndexable()); FirebaseUserActions.getInstance().start(getAction()); } @Override protected void onStop() { FirebaseUserActions.getInstance().end(getAction()); super.onStop(); }