Add the App Indexing API

The App Indexing API takes a history of actions to your app, such as TYPE_VIEW for static content or TYPE_WATCH for video playback. It sends that history to our systems along with content fields that you provide, such as titles and descriptions. This enables query autocompletions for your app users, as well as richer search results, improved Search quality, and enhanced ranking signals. Users can view and delete past activity in apps at https://history.google.com/.

The following steps describe how to integrate App Indexing in your app. Use the Android Studio 1.5 Code Generation utility to simplify your workflow.

Add Google Play services

manifest
<application>
  ...
  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  ...
</application>
      
build.gradle
dependencies {
  ...
  compile 'com.google.android.gms:play-services-appindexing:8.4.0'
  ...
}
      

Import classes

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
  

Add App Indexing API calls

Define the title, description, URL, and type for relevant activities and build the App Indexing object. This enables search results to show your preferred app page title and description, and to serve richer results like play actions.

...
public class MainActivity extends Activity {
  private GoogleApiClient mClient;
  private Uri mUrl;
  private String mTitle;
  private String mDescription;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    mUrl = "http://examplepetstore.com/dogs/standard-poodle";
    mTitle = "Standard Poodle";
    mDescription = "The Standard Poodle stands at least 18 inches at the withers";
  }

  public Action getAction() {
    Thing object = new Thing.Builder()
        .setName(mTitle)
        .setDescription(mDescription)
        .setUrl(mUrl)
        .build();

    return new Action.Builder(Action.TYPE_VIEW)
        .setObject(object)
        .setActionStatus(Action.STATUS_TYPE_COMPLETED)
        .build();
  }

Indicate the app activity

Call activity start after the view completely renders, passing in the App Indexing object defined above. Call the AppIndexAPI.end() method after the activity completes, and disconnect your client.

  @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();
  }
Next: test your implementation

Send feedback about...

App Indexing
App Indexing