blob: 2c78064ea4cccf832c9b872ba3e37e1a91282271 [file] [log] [blame]
page.title=Making Recommendations
<div id="qv-wrapper">
<div id="qv">
<h2>In this document</h2>
<li><a href="#service">Create a Recommendations Service</a></li>
<li><a href="#build">Build Recommendations</a></li>
<li><a href="#run-service">Run Recommendations Service</a></li>
<li><a href="#DesignLandscapeLayouts">Design Landscape Layouts</a></li>
<p>Content recommendations appear as the first row of the TV launch screen after the first use
of the device. This row is intended to help users quickly find content they enjoy. Contributing
recommendations from your apps content catalog can help bring users back to your app.</p>
<img src="{@docRoot}preview/tv/images/home-recommendations.png" alt="" id="figure1" />
<p class="img-caption">
<strong>Figure 1.</strong> An example of the recommendations row.
<h2 id="service">Create a Recommendations Service</h2>
<p>Content recommendations are created with background processing. In order for your application
to contribute to recommendations, you create a service that periodically adds listings from your
app's catalog to the system list of recommendations.</p>
<p>The following code example illustrates how to extend the {@link} to
create a recommendation service for your application.</p>
public class RecommendationsService extends IntentService {
private static final int MAX_RECOMMENDATIONS = 3;
public RecommendationsService() {
protected void onHandleIntent(Intent intent) {
MovieDatabase database = MovieDatabase.instance(getApplicationContext());
List<Movie> recommendations = database.recommendations();
int count = 0;
try {
for (Movie movie : recommendations) {
// build the individual content recommendations
buildRecommendation(getApplicationContext(), movie);
if (++count >= MAX_RECOMMENDATIONS) {
} catch (IOException e) {
Log.e(TAG, "Unable to update recommendation", e);
<p>In order for this class to be recognized and run as a service, you must register this service
using your app manifest. The following code snippet illustrates how to add this class as a
&lt;manifest ... &gt;
&lt;application ... &gt;
&lt;service android:name=&quot;.RecommendationsService&quot;
android:enabled=&quot;true&quot; android:exported=&quot;true&quot;/&gt;
<h2 id="build">Build Recommendations</h2>
<p>Once it starts running, your service must create recommendations and pass them to the Android
framework. The framework receives the recommendations as {@link} objects
that use a specific style and are marked with a specific category.</p>
<p>The following code example demonstrates how to get an instance of the {@link}, build a recommendation, and post it to the manager:</p>
public class RecommendationsService extends IntentService {
public Notification buildRecommendation(Context context, Movie movie)
throws IOException {
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager)
Bundle extras = new Bundle();
if (mBackgroundUri != movie.getBackgroundUri()) {
extras.putString(EXTRA_BACKGROUND_IMAGE_URL, movie.getBackgroundUri());
// build the recommendation as a Notification object
Notification notification = new NotificationCompat.BigPictureStyle(
new NotificationCompat.Builder(context)
// post the recommendation to the NotificationManager
mNotificationManager.notify(movie.getId(), notification);
mNotificationManager = null;
return notification;
private PendingIntent buildPendingIntent(long id) {
Intent detailsIntent = new Intent(this, DetailsActivity.class);
detailsIntent.putExtra("id", id);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Ensure each PendingIntent is unique
PendingIntent intent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
return intent;
<h3 id="run-service">Run Recommendations Service</h3>
<p>Your app's recommendation service must run periodically in order to create current
recommendations. In order to run your service, you should create a class that runs a timer and
invokes it at regular intervals. The following code example extends the {@link
android.content.BroadcastReceiver} class to start periodic execution of a recommendation service
every 12 hours:</p>
public class BootupReceiver extends BroadcastReceiver {
private static final String TAG = "BootupActivity";
private static final long INITIAL_DELAY = 5000;
public void onReceive(Context context, Intent intent) {
if (intent.getAction().endsWith(Intent.ACTION_BOOT_COMPLETED)) {
private void scheduleRecommendationUpdate(Context context) {
AlarmManager alarmManager = (AlarmManager)context.getSystemService(
Intent recommendationIntent = new Intent(context,
PendingIntent alarmIntent = PendingIntent.getService(context, 0,
recommendationIntent, 0);
<p>In order for the {@link android.content.BroadcastReceiver} class to execute after a TV
device starts up, you must register this class in your app manifest and attach an intent filter
in order for the device boot process to complete. This sample code demonstrates how to add this
configuration to the manifest:</p>
&lt;manifest ... &gt;
&lt;application ... &gt;
&lt;receiver android:name=&quot;.BootupReceiver&quot; android:enabled=&quot;true&quot;
&lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot;/&gt;
<p class="important">
<strong>Important:</strong> Receiving a boot completed notification requires that your app
request the {@link android.Manifest.permission#RECEIVE_BOOT_COMPLETED} permission.
For more information, see {@link android.content.Intent#ACTION_BOOT_COMPLETED}.