我似乎无法创建一个Android Wear通知,而不会使应用程序图标闪烁,而在Android手机上相同的代码却正常工作。
大多数参考解决方案都涉及更新相同的通知、使用setAlertOnlyOnce、保持ID或when不变。然而,无论我做什么,每次更新通知时它都会闪烁(最常见于应用程序图标)。
如Android Wear: Timer like notification card on wear device所建议的,您可以使用setHintHideIcon(true)隐藏应用程序图标,这将隐藏闪烁部分,但是在Android Wear通知的有限世界中,应用程序图标在应用品牌中扮演着重要角色。
如果您想要一个计时器,您可以使用.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)
问题出现时机:在作为可穿戴应用程序运行或作为显示在可穿戴设备上的手机通知时。在手机上完美运行。