本頁面由 Cloud Translation API 翻譯而成。
Switch to English

遷移到最新的API

如果您當前使用的Firebase App Indexing API版本早於10.0.0,並且想要移至最新版本,請按照以下步驟操作。

在開始之前,請確保已將Firebase添加到您的應用程序。

更新應用程序索引庫

轉到應用程序的build.gradle腳本並添加以下依賴項:

dependencies {
  ...
  implementation 'com.google.firebase:firebase-appindexing:19.1.0'
  ...
}
      

更新活動中的導入

      // Before
      import 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 this
  private GoogleApiClient mClient;
  ...

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    // Delete this
   mClient = new GoogleApiClient.Builder(this)
        .addApi(AppIndex.API)
        .build();
    ...
   }
 }
 

更新用戶操作構建器

      //Before
      public 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. See
   https://firebase.google.com/docs/app-indexing/android/personal-content#update-the-index for
   adding content to the index */
   FirebaseAppIndex.getInstance().update(getIndexable());
   FirebaseUserActions.getInstance().start(getAction());
}

@Override
protected void onStop() {
   FirebaseUserActions.getInstance().end(getAction());
   super.onStop();
}