Android Studio V3.12环境下TV开发教程(十五) Android N及更早版本中的建议

Android Studio V3.12环境下TV开发教程

(转自Android官网https://developer.android.com/training/tv/start)

文章源自:光谷佳武 https://blog.csdn.net/jiawuhan/article/details/80647902


Android N及更早版本中的建议

在与电视进行互动时,用户通常更愿意在观看内容之前输入最少的内容。 对于许多电视用户而言,理想的场景是:坐下,打开并观看。 获得用户喜欢的内容的最少步骤通常是他们喜欢的路径。

注意:使用此处描述的API仅对在Android版本(包括Android 7.1(API级别25))中运行的应用程序提供建议。 要为Android 8.0(API级别26)及更高版本中运行的应用提供建议,您的应用必须使用推荐渠道 

Android框架通过在主屏幕上提供建议行来协助最小输入交互。 内容建议在首次使用设备后显示为电视主屏幕的第一行。来自应用内容目录的推荐建议可以帮助用户回到您的应用。

图1.推荐行的示例。

本课将告诉您如何创建建议并将其提供给Android框架,以便用户可以轻松发现并享受您的应用内容。 本讨论描述了Android Leanback示例应用程序的一些代码。

建议的最佳做法

建议可帮助用户快速找到他们喜欢的内容和应用。 创建高质量且与用户相关的建议是为您的电视应用创造出色用户体验的重要因素。 出于这个原因,您应该仔细考虑向用户提供哪些建议并密切管理它们。

建议的类型

当您创建建议时,应该将用户链接回不完整的查看活动或建议将其扩展到相关内容的活动。 以下是您应该考虑的一些特定类型的建议:

  • 为下一集的用户继续观看一系列内容的续集内容推荐。 或者,对暂停的电影,电视节目或播客使用延续建议,以便用户只需点击几下鼠标即可回到观看暂停的内容。
  • 如果用户看完其他系列节目,则可以提供新的内容建议,例如新的首播剧集。 此外,如果您的应用允许用户订阅,跟踪或跟踪内容,则可以在其跟踪内容列表中使用针对未浏览过的内容的新内容推荐。
  • 相关内容建议基于用户的历史观看行为。

有关如何设计推荐卡以获得最佳用户体验的更多信息,请参阅Android TV设计规范中的推荐行 

刷新建议

刷新建议时,不要只删除并重新发布它们,因为这样会使建议显示在建议行的末尾。 一旦播放了内容项目(如电影), 请将其从建议中移除 

自定义建议

您可以通过设置用户界面元素(如卡的前景和背景图像,颜色,应用程序图标,标题和副标题)来自定义推荐卡以传达品牌信息。 要了解更多信息,请参阅Android TV设计规范中的推荐行 

小组建议

您可以选择根据推荐来源对推荐进行分组。 例如,您的应用可能会提供两组建议:用户订阅的内容建议,以及用户可能不知道的新趋势内容推荐。

系统在创建或更新推荐行时分别对每个组的建议进行排序和排序。 通过为您的推荐提供组信息,您可以确保您的推荐不会按照无关的建议进行排序。

使用NotificationCompat.Builder.setGroup()设置建议的组密钥字符串。 例如,要将推荐标记为属于包含新趋势内容的组,您可以调用setGroup("trending") 

创建一个推荐服务

内容建议是通过后台处理创建的。 为了让您的应用程序对建议有所贡献,请创建一项服务,定期将应用程序目录中的列表添加到系统的建议列表中。

以下代码示例说明如何扩展IntentService以为您的应用程序创建推荐服务:

公共类UpdateRecommendationsService扩展IntentService {
     private static final String TAG =“UpdateRecommendationsService”;
     private static final int MAX_RECOMMENDATIONS = 3;

     public UpdateRecommendationsService(){
        超级( “RecommendationService”);
     }

     @覆盖
     protected void onHandleIntent(Intent intent){
         Log.d(TAG,“更新推荐卡”);
         HashMap <String,List <Movie >> recommendations = VideoProvider.getMovieList();
         if(recommendations == null)return;

         int count = 0;

        尝试{
             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(电影))
                             。建立();

                    如果(++ count> = MAX_RECOMMENDATIONS){
                        打破;
                     }
                 }
                如果(++ count> = MAX_RECOMMENDATIONS){
                    打破;
                 }
             }
         } catch(IOException e){
             Log.e(TAG,“无法更新推荐”,e);
         }
     }

    私人PendingIntent buildPendingIntent(电影电影){
         Intent detailsIntent = new Intent(this,DetailsActivity.class);
         detailsIntent.putExtra(“电影”,电影);

         TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
         stackBuilder.addParentStack(DetailsActivity.class);
         stackBuilder.addNextIntent(detailsIntent);
         //确保唯一的PendingIntents,否则全部
         //建议以相同的PendingIntent结束
         detailsIntent.setAction(Long.toString(movie.getId()));

         PendingIntent intent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
        返回意图;
     }
 }

为了使系统识别此服务并运行,请使用您的应用程序清单进行注册。 以下代码片段演示了如何将此类声明为服务:

 <manifest ...>
   <应用程序...>
     ...

     <服务
            机器人:名字= “com.example.android.tvleanback.UpdateRecommendationsService”
             android:enabled =“true”/>
   </应用>
 </清单>

建立建议

一旦您的推荐服务开始运行,它必须创建建议并将其传递给Android框架。 框架接收建议作为使用特定模板并用特定类别标记的Notification对象。

设置值

要为推荐卡设置UI元素值,请创建一个遵循以下所述的构建器模式的构建器类。 首先,您设置推荐卡片元素的值。

公共类RecommendationBuilder {
     ...

     public RecommendationBuilder setTitle(String title){
             mTitle = title;
            返回这个;
         }

         public RecommendationBuilder setDescription(String description){
             mDescription = description;
            返回这个;
         }

         public RecommendationBuilder setImage(String uri){
             mImageUri = uri;
            返回这个;
         }

         public RecommendationBuilder setBackground(String uri){
             mBackgroundUri = uri;
            返回这个;
         }
 ...

创建通知

一旦设置了值,就可以构建通知,将构建器类中的值分配给通知,并调用NotificationCompat.Builder.build() 

此外,一定要调用setLocalOnly()以便NotificationCompat.BigPictureStyle通知不会显示在其他设备上。

以下代码示例演示如何构建建议。

公共类RecommendationBuilder {
     ...

    公共Notification构建()抛出IOException {
         ...

        通知通知= new NotificationCompat.BigPictureStyle(
                新的NotificationCompat.Builder(mContext)
                         .setContentTitle(mTitle)
                         .setContentText(mDescription)
                         .setPriority(mPriority)
                         .setLocalOnly(真)
                         .setOngoing(真)
                         .setColor(mContext.getResources()。的getColor(R.color.fastlane_background))
                         .setCategory(Notification.CATEGORY_RECOMMENDATION)
                         .setLargeIcon(图像)
                         .setSmallIcon(mSmallIcon)
                         .setContentIntent(mIntent)
                         .setExtras(演员))
                 。建立();

        退货通知;
     }
 }

运行建议服务

您的应用推荐服务必须定期运行以创建当前建议。 要运行你的服务,创建一个运行定时器的类并定期调用它。 以下代码示例扩展了BroadcastReceiver类,以便每半小时开始一次推荐服务的定期执行:

公共类BootupActivity扩展BroadcastReceiver {
     private static final String TAG =“BootupActivity”;

    私人静态最终长期INITIAL_DELAY = 5000;

     @覆盖
     public void onReceive(Context context,Intent intent){
         Log.d(TAG,“BootupActivity initiated”);
         if(intent.getAction()。endsWith(Intent.ACTION_BOOT_COMPLETED)){
             scheduleRecommendationUpdate(上下文);
         }
     }

     private void scheduleRecommendationUpdate(Context context){
         Log.d(标签,“调度建议更新”);

         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类的这种实现必须在安装它的电视设备启动后运行。 要做到这一点,请在应用程序清单中注册此类,并使用意图过滤器来侦听设备引导过程的完成情况。 以下示例代码演示了如何将此配置添加到清单:

 <manifest ...>
   <应用程序...>
     <receiver android:name =“com.example.android.tvleanback.BootupActivity”
              机器人:启用=“真”
              机器人:导出=“假”>
       <意图滤波器>
         <action android:name =“android.intent.action.BOOT_COMPLETED”/>
       </意图滤波器>
     </接收机>
   </应用>
 </清单>

重要提示:接收启动完成通知要求您的应用程序请求RECEIVE_BOOT_COMPLETED权限。 有关更多信息,请参阅ACTION_BOOT_COMPLETED 

在您的推荐服务类' onHandleIntent()方法中,将建议发布给经理,如下所示:

通知通知= notificationBuilder.build();
 mNotificationManager.notify(id,notification);


猜你喜欢

转载自blog.csdn.net/jiawuhan/article/details/80647902