Обновлять RemoteViews в уведомлении каждую секунду

У меня проблема с обновлением пользовательского значения уведомления в моем сервисе. Я использую RemoteView и хочу обновлять текстовое представление каждую секунду, и у меня нет никакой истинной идеи сделать это. это мой код:

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);

любая идея?

1 ответ

Я до сих пор не совсем понял, как правильно обновлять текст уведомления, НО я заметил, что обновление TextView в RemoteView вызовет некоторые проблемы с производительностью, а также значительно замедлит общий пользовательский интерфейс. В любом случае... после возни с уведомлением это в настоящее время, что у меня есть:

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);

При использовании этого метода у вас не должно быть проблем с производительностью. Я все еще работаю над этим, если я что-то выясню плохо, дайте вам знать.

Другие вопросы по тегам