<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > 編寫:[awong1900](https://github.com/awong1900) - 原文:[http://developer.android.com/training/tv/discovery/recommendations.html](http://developer.android.com/training/tv/discovery/recommendations.html) 當操作TV時,用戶通常喜歡使用最少的輸入操作來找內容。許多用戶的理想場景是,坐下,打開TV然后觀看。用最少的步驟讓用戶觀看他們的喜歡的內容是最好的方式。 安卓framework為了實現少交互而提供了主屏幕推薦欄。在設備第一次使用時候,內容推薦出現在TV主屏幕的第一欄。從你的應用程序的內容目錄提供建議可以幫助把用戶帶回到你的應用程序。 ![home-recommendations](https://box.kancloud.cn/2015-07-28_55b724748bde1.png) 圖1. 一個推薦欄的例子 這節課教你如何創建推薦和提供他們到安卓framework,這樣用戶能容易的發現和使用你的應用內容。這個討論描述了一些代碼,在[安卓Leanback示例代碼](https://github.com/googlesamples/androidtv-Leanback)。 ### 創建推薦服務 內容推薦是被后臺處理創建。為了把你的應用去提供到推薦,創建一個周期性添加列表服務,從應用目錄到系統推薦列表。 接下來的代碼描繪了如何擴展[IntentService](http://developer.android.com/reference/android/app/IntentService.html)為你的應用創建推薦服務: ~~~ public class UpdateRecommendationsService extends IntentService { private static final String TAG = "UpdateRecommendationsService"; private static final int MAX_RECOMMENDATIONS = 3; public UpdateRecommendationsService() { super("RecommendationService"); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, "Updating recommendation cards"); HashMap<String, List<Movie>> recommendations = VideoProvider.getMovieList(); if (recommendations == null) return; int count = 0; try { RecommendationBuilder builder = new RecommendationBuilder() .setContext(getApplicationContext()) .setSmallIcon(R.drawable.videos_by_google_icon); for (Map.Entry<String, List<Movie>> entry : recommendations.entrySet()) { for (Movie movie : entry.getValue()) { Log.d(TAG, "Recommendation - " + movie.getTitle()); builder.setBackground(movie.getCardImageUrl()) .setId(count + 1) .setPriority(MAX_RECOMMENDATIONS - count) .setTitle(movie.getTitle()) .setDescription(getString(R.string.popular_header)) .setImage(movie.getCardImageUrl()) .setIntent(buildPendingIntent(movie)) .build(); if (++count >= MAX_RECOMMENDATIONS) { break; } } if (++count >= MAX_RECOMMENDATIONS) { break; } } } catch (IOException e) { Log.e(TAG, "Unable to update recommendation", e); } } private PendingIntent buildPendingIntent(Movie movie) { Intent detailsIntent = new Intent(this, DetailsActivity.class); detailsIntent.putExtra("Movie", movie); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(DetailsActivity.class); stackBuilder.addNextIntent(detailsIntent); // Ensure a unique PendingIntents, otherwise all recommendations end up with the same // PendingIntent detailsIntent.setAction(Long.toString(movie.getId())); PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); return intent; } } ~~~ 為了服務被系統意識到和運行,在應用manifest中注冊它,接下來的代碼片段展示了如何定義這個類的服務: ~~~ <manifest ... > <application ... > ... <service android:name="com.example.android.tvleanback.UpdateRecommendationsService" android:enabled="true" /> </application> </manifest> ~~~ ### 刷新推薦 基于用戶的行為和數據來推薦,例如播放列表,喜愛列表和相關內容。當刷新推薦時,不僅僅是刪除和重新加載他們,因為這樣導致推薦出現在推薦欄的結尾。一旦一個內容項被播放,如一個影片,從推薦中[刪除它](http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Removing)。 應用的推薦被保存依據哪個應用提供他們。framework interleave應用推薦基于推薦質量,用戶習慣的收集。最好的推薦使應用推薦更幸運的出現在列表前面。 ### 創建推薦 一旦你的推薦服務開始運行,它必須創建推薦和送他們到安卓framework。Framework收到推薦作為[通知](http://developer.android.com/reference/android/app/Notification.html)對象。它用特定的模板并且標記為特定的目錄。 ### 設置值 去設置推薦卡片的UI元素,創建一個builder類用接下來的builder樣式描述。首先,設置推薦卡片元素的值。 ~~~ public class RecommendationBuilder { ... public RecommendationBuilder setTitle(String title) { mTitle = title; return this; } public RecommendationBuilder setDescription(String description) { mDescription = description; return this; } public RecommendationBuilder setImage(String uri) { mImageUri = uri; return this; } public RecommendationBuilder setBackground(String uri) { mBackgroundUri = uri; return this; } ... ~~~ ### 創建通知 一旦你設置了值,然后去創建通知,從builder類分配值到通知,并且調用[NotificationCompat.Builder.build](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#build())。 并且,確信調用[setLocalOnly()](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLocalOnly(boolean)),這樣[NotificationCompat.BigPictureStyle](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.BigPictureStyle.html)通知不將顯示在另一個設備。 接下來的代碼示例展示了如何創建推薦。 ~~~ public class RecommendationBuilder { ... public Notification build() throws IOException { ... Notification notification = new NotificationCompat.BigPictureStyle( new NotificationCompat.Builder(mContext) .setContentTitle(mTitle) .setContentText(mDescription) .setPriority(mPriority) .setLocalOnly(true) .setOngoing(true) .setColor(mContext.getResources().getColor(R.color.fastlane_background)) .setCategory(Notification.CATEGORY_RECOMMENDATION) .setLargeIcon(image) .setSmallIcon(mSmallIcon) .setContentIntent(mIntent) .setExtras(extras)) .build(); return notification; } } ~~~ ### 運行推薦服務 你的應用推薦服務必須周期性運行,原因是創建當前的推薦。去運行你的服務,創建一個類運行計時器和在周期間隔關聯它。接下來的代碼例子擴展了[BroadcastReceiver](http://developer.android.com/reference/android/content/BroadcastReceiver.html)類去開始每半小時的推薦服務的周期性執行: ~~~ public class BootupActivity extends BroadcastReceiver { private static final String TAG = "BootupActivity"; private static final long INITIAL_DELAY = 5000; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "BootupActivity initiated"); if (intent.getAction().endsWith(Intent.ACTION_BOOT_COMPLETED)) { scheduleRecommendationUpdate(context); } } private void scheduleRecommendationUpdate(Context context) { Log.d(TAG, "Scheduling recommendations update"); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent recommendationIntent = new Intent(context, UpdateRecommendationsService.class); PendingIntent alarmIntent = PendingIntent.getService(context, 0, recommendationIntent, 0); alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, INITIAL_DELAY, AlarmManager.INTERVAL_HALF_HOUR, alarmIntent); } } ~~~ 這個[BroadcastReceiver](http://developer.android.com/reference/android/content/BroadcastReceiver.html)類的實現必須運行在TV設備啟動后。 去完成這個,注冊這個類在應用manifest的intet filter中,它監聽設備啟動完成。接下來的代碼展示了如何添加這個配置到manifest。 ~~~ <manifest ... > <application ... > <receiver android:name="com.example.android.tvleanback.BootupActivity" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> </application> </manifest> ~~~ > **Important**: 接收一個啟動完成通知需要你的應用有[RECEIVE_BOOT_COMPLETED](http://developer.android.com/reference/android/Manifest.permission.html#RECEIVE_BOOT_COMPLETED)權限。更多信息,查看[ACTION_BOOT_COMPLETED](http://developer.android.com/reference/android/content/Intent.html#ACTION_BOOT_COMPLETED)。 在推薦服務類的[onHandleIntent()](http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent))方法,提交推薦到管理器,如下: ~~~ Notification notification = notificationBuilder.build(); mNotificationManager.notify(id, notification); ~~~ [下一節: 使TV應用可搜索](#)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看