【问题标题】:update RemoteViews in notification every second每秒更新一次通知中的 RemoteViews
【发布时间】:2015-07-07 09:08:41
【问题描述】:

我在更新服务中的自定义通知值时遇到问题。 我使用 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);

有什么想法吗?

【问题讨论】:

  • update textview every second - 这是个非常糟糕的主意。
  • @MarcinOrlowski 您对更新通知有何建议?

标签: java android android-remoteview


【解决方案1】:

我还没有完全弄清楚如何正确更新通知文本,但我注意到在 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);

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

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 2015-04-16
    • 2017-11-27
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多