The Android App Indexing API indexes user actions in your app, such as
TYPE_VIEW
. This lets your users see app pages they have visited as result in
Search autocomplete. Users can view and delete past activity in apps at
https://history.google.com/.
The following steps describe how to integrate App Indexing API in your app. Use the Android Studio 2.x Code Generation utility to simplify your workflow.
Add Google Play services
dependencies { ... compile 'com.google.android.gms:play-services-appindexing:9.6.1' ... }
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 Action 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