gpt4 book ai didi

Android Wear 通知在更新时防止图标闪烁

转载 作者:IT王子 更新时间:2023-10-28 23:41:40 26 4
gpt4 key购买 nike

我似乎无法创建一个在不闪烁应用图标的情况下更新的 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?

最佳答案

替换:

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

到:

NotificationManagerCompat notifyMgr =
NotificationManagerCompat.from(this);

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

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

关于Android Wear 通知在更新时防止图标闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27430821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com