【问题标题】:Android TV - Recommendation (Notification) will not auto-cancelAndroid TV - 推荐(通知)不会自动取消
【发布时间】:2014-12-16 18:09:03
【问题描述】:

在 Android TV 上的 Lollipop 中创建推荐(或通知)时,我无法将其设置为自动取消。

我正在使用 Android TV 开发者页面中推荐的“NotificationCompat.BigPictureStyle”。通知按设计工作,按预期触发 PendingIntent,但不会自动取消并从推荐栏中消失。第二次选择推荐会出现一个空白屏幕,所以我猜当时 PendingIntent 为空。 (ADB 在第二次调用时显示 android.content.IntentSender$SendIntentException。)

在 Nexus Player 和 Android TV 模拟器上测试。

private void buildAndroidTVRecommendation(String name, PendingIntent pIntent,
        Context context2, Bundle extras) {

     NotificationManager mNotificationManager = (NotificationManager)
             context2.getSystemService(Context.NOTIFICATION_SERVICE);
     Bitmap smallBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.air_share);

    Notification notification = new NotificationCompat.BigPictureStyle(
            ( new NotificationCompat.Builder(context)
                    .setContentTitle("Air-Share - incoming share")
                    .setContentText("From: "+name)
                    .setContentInfo("Air-Share"))
                    .setGroup("Air-Share")
                    .setColor(0xFFFF2020)
                    .setCategory(Notification.CATEGORY_RECOMMENDATION)
                    .setLargeIcon(smallBitmap)
                    .setSmallIcon(R.drawable.air_share)
                    .setContentIntent(pIntent)
                    .setExtras(extras)
                    .setAutoCancel(true)

                    )
            .build();

    mNotificationManager.notify(pendingCounter, notification);
    mNotificationManager = null;


}

【问题讨论】:

  • 您希望它何时自动取消?当涉及刷新、切换位置被解除/更新时,该建议由系统处理。
  • 我期待与主流设备 Android 通知类似的自动取消行为。例如在 Jellybean 平板电脑上,当用户拉下通知栏并按下通知(自动取消标志 = true)时,pendingIntent 被激活并且通知从系统中消失。
  • 正如我所说,Android TV 可能并非如此。建议由系统处理,因此您不能决定是否应忽略您的内容。其他应用(例如 YouTube、DailyMotion、musixmatch 等)也与此行为一致。
  • 或许你有道理,但为什么在第二次选择推荐时会产生空白屏幕和异常?
  • 这听起来更像是您这边出了点问题。我在构建公司的应用程序 (musixmatch) 时没有遇到过这个问题。

标签: android android-5.0-lollipop android-tv


【解决方案1】:

为了解决这个问题,我创建了一个解决方法。

我创建了一个新 Activity,其唯一目的是接收来自 Recommendation 的 Pending Intent。 我更改了建议的原始待定意图以调用此新活动而不是我想要的活动。 (所需的活动在我的应用程序之外。)在 Extras 中,我捆绑了我需要知道的所有内容,以实现我最初所需的意图以及通知 ID。 当新活动启动时(用户点击推荐后),我提取 ID 并取消推荐。然后我提取所需意图的信息,创建意图并最终完成活动。

public class TVRecommendationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent in = (Intent) getIntent();
        String jsonString = in.getStringExtra("jsonString");
        String fileUri = in.getStringExtra("fileUri");

        int id  =in.getIntExtra("notificationID", -1);

        NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if (id >= 0 ) nm.cancel(id);

        JSONIntent newIntent = new JSONIntent(getApplicationContext());
        newIntent.setIncomingLocalFileURI(fileUri);
        Intent out = newIntent.buildIntentFromJSON(jsonString, fileUri);

        if (out != null) startActivity(out);
        finish();
    }

}

【讨论】:

  • FWIW 我有同样的问题,但缺少setAutoCancel(),添加导致我的通知自动关闭。 (Nexus Player 5.1.1 LMY47V)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 1970-01-01
相关资源
最近更新 更多