每秒更新通知中的远程视图

rn0zuynd  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(203)

我在更新服务中的自定义通知值时遇到问题。我使用remoteview,并希望每秒钟更新textview,但我没有任何真正的想法这样做。这是我的密码:

int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

contentView = new RemoteViews(getPackageName(), R.layout.notificationview);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
contentView.setTextViewText(R.id.text, "This is a custom layout"); 
contentView.setTextViewText(R.id.title, "Title");
notification.contentView = contentView;

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; 

startForeground(13, notification);

你知道吗?

eulz3vhy

eulz3vhy1#

我还没有完全弄清楚如何正确地更新通知文本,但是我注意到,在remoteview中更新textview会导致一些性能问题,而且还会导致整个ui速度大大降低。不管怎样。。。在搞乱了通知之后,我现在得到的是:

NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(activity);

notiBuilder.setContentIntent(pendingIntent)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("Sup bro!")
    .setContentText("It works dude!")
    .addAction(R.drawable.icon, "Some Text", pendingAction);

NotificationManager.notify(NOTIFICATION_ID, notify);

然后你可以像这样更新通知文本:

notiBuilder.setContentText("Updating text...");
NotificationManager.notify(NOTIFICATION_ID, notify);

使用这种方法,您不应该有任何性能问题。我还在努力,如果我发现什么,我会告诉你的。

相关问题