【问题标题】:Android Wear notification prevent blinking icon when updatingAndroid Wear 通知在更新时防止闪烁图标
【发布时间】:2014-12-11 19:26:23
【问题描述】:

我似乎无法创建一个在不闪烁应用图标的情况下更新的 Android Wear 通知,而相同的代码在 Android 手机上运行良好。

大多数引用的解决方案都谈到更新相同的通知,使用 setAlertOnlyOnce,保持 IDwhen 相同。但是,无论我做什么,每次更新通知时它都会闪烁(最常见的是应用图标)。

正如这里Android Wear: Timer like notification card on wear device 建议的那样,您可以使用 setHintHideIcon(true) 隐藏应用程序图标,这会隐藏闪烁的部分,但是在 Android Wear Notifications 的有限世界中,应用程序图标会发挥很大的作用参与应用程序的品牌塑造。

如果你想要一个计时器,你可以使用 .setUsesChronometer(true) 并让系统更新完美运行的计时器。不幸的是,如果您想更新时间以外的其他内容(例如收到的步数或消息计数),在我看来您不走运。

您可以在下面找到作为手机应用运行时运行良好但作为可穿戴应用运行时闪烁的代码。

下面的注释行表明通知仍然闪烁(在可穿戴设备上运行时,而不是手机),当在可穿戴设备上发布未更改的通知时,通知仍然闪烁。取消注释以再次更新通知。

mNotification = buildNotification(WearMainActivity.this);

因此我的问题是,是否有人有任何进一步的想法,我们可以探索以防止通知闪烁,或者我们是否可以将其写为 Android Wear 错误?

public class WearMainActivity extends Activity {

    public final int NOTIFICATION_ID= 1;
    public Notification mNotification;
    public int count;
    public long when;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        count = 0;
        when = System.currentTimeMillis();
        mNotification = buildNotification(WearMainActivity.this);
        postDelayedHandler();
        finish();
    }

    private void postDelayedHandler(){

        new Handler().postDelayed(new Runnable() {
            public void run() {
                count++;
                mNotification = buildNotification(WearMainActivity.this);
                NotificationManager notifyMgr = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
                notifyMgr.notify(NOTIFICATION_ID, mNotification);
                postDelayedHandler();
            }
        }, 1000L);
    }

    private Notification buildNotification(Context context){
        return new Notification.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText("Count: "+count)
                .setWhen(when)
//                .setOngoing(true) //Don't do this, adds "Mute app" action
                .setOnlyAlertOnce(true)
                .setPriority(Notification.PRIORITY_MAX)
                .extend(new Notification.WearableExtender()
//                        .setHintHideIcon(true) //Hides the icon, so kinda hides the blink
                )
                .build();
    }
}

测试于:
可穿戴:Moto 360 (4.4W2) Wear Emulator (5.0.1)
手机:Galaxy Nexus (4.3) 和 Nexus 5 (5.0.0)

发生:作为可穿戴应用程序运行或作为可穿戴设备上显示的电话通知时。在手机上完美运行。

参考问题:
How can I avoid blinking notification update while changing button
Updating an ongoing notification quietly
How to properly update a notification post api 11?

【问题讨论】:

  • 很抱歉,但我不太明白 - 自动镜像到手表的手机应用程序(无磨损组件)中的定期通知是否也会发生这种情况?因为我用你的代码测试了这个场景,没有看到闪烁(Moto 360 with Android 5.0)。
  • 啊,感谢您注意到这一点。显然,当您在手机上更新通知时,系统足够智能,不会向可穿戴设备发送未更改的通知。为了澄清我已经制作了一个关于这种行为的视频:youtube.com/watch?v=8QBuUjb_vTA
  • 更新:这似乎不会在运行 5.0 的 Gear Live 上发生。所以有两个选择:a)它不会在那里闪烁,因为它的硬件更好 b)它可能会在 5.0 中修复。现在等待我的手表接收更新以确认 a 或 b...(哦,我希望我可以只加载 OTA ;))
  • 我会赌 (b)。我们会看到:)
  • 所以我刚刚获得了 5.0 更新(花了足够长的时间!)答案是:是的,不是的!当您从手机上启动的应用程序收到扩展通知时,它会无缝运行,不会闪烁。但是,如果您从手表启动应用程序,它仍然会闪烁。所以现在我们根据通知的来源有两种不同的行为......

标签: android wear-os android-wear-notification


【解决方案1】:

替换:

NotificationManager notifyMgr = 
    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE));

到:

NotificationManagerCompat notifyMgr =
    NotificationManagerCompat.from(this);

更多信息:https://developer.android.com/training/wearables/notifications/creating.html

您还进行了很多更新。每次更新都通过蓝牙发送到 Wear。您应该为 Android Wear 创建自安装应用程序。发送延迟约3秒。

【讨论】:

    【解决方案2】:

    不久前我在使用 Android Wear 时解决了这个问题,但不幸的是其中的代码不见了。无论如何,我所做的并不是每次我想更新它时都构建通知,我只是在第一次创建通知时标记它,然后我用TAG 检索它并直接对该对象进行更改。这完全停止了闪烁......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多