Add AppIndexing API
suggest changeThe official documentation covers the full setup. This example highlights the parts that are easy to get wrong.
Step 1 :- Add google service
dependencies {
...
compile 'com.google.android.gms:play-services-appindexing:9.4.0'
...
}
Step 2 :- Import classes
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
Step 3 :- Add App Indexing API calls
private GoogleApiClient mClient;
private Uri mUrl;
private String mTitle;
private String mDescription;
// If the values to index are already known, initialize them in onCreate().
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
mUrl = Uri.parse("http://examplepetstore.com/dogs/standard-poodle");
mTitle = "Standard Poodle";
mDescription = "The Standard Poodle stands at least 18 inches at the withers";
}
// If the data comes from a network request, set these fields when the response arrives
// and check for null so a failed response does not crash the app.
void onIndexableContent(String id, String title, String description) {
mUrl = Uri.parse("android-app://com.famelive/https/m.fame.live/vod/" + id);
mTitle = title;
mDescription = description;
mClient.connect();
AppIndex.AppIndexApi.start(mClient, getAction());
}
@Override
protected void onStop() {
// If the response failed, these fields may still be null.
if (mTitle != null && mDescription != null && mUrl != null && getAction() != null) {
AppIndex.AppIndexApi.end(mClient, getAction());
mClient.disconnect();
}
super.onStop();
}
public Action getAction() {
Thing object = new Thing.Builder()
.setName(mTitle)
.setDescription(mDescription)
.setUrl(mUrl)
.build();
return new Action.Builder(Action.TYPE_WATCH)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
To test this just follow the step 4 in Remarks given below.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents