2015-04-28 53 views
2

我在更新服務中的自定義通知值時遇到問題。 我使用RemoteView並希望每秒更新textview,而且我沒有任何真正的想法來做到這一點。 這是我的代碼:更新每秒通知中的RemoteViews

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

有什麼想法嗎?

+0

'每秒更新textview' - 這是一個非常糟糕的主意。 –

+0

@MarcinOrlowski你有什麼建議更新通知? – meysam

回答

2

我還沒有完全想出如何正確更新通知文本,但我注意到,在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); 

任何性能問題。我仍在努力,如果我找出生病讓你知道。