2

サービスのカスタム通知値の更新に問題があります。私は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);

何か案が ?

4

1 に答える 1

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

この方法を使用すると、パフォーマンスの問題は発生しません。私はまだそれに取り組んでいます。何か悪いことがわかったらお知らせください。

于 2015-09-21T04:59:55.847 に答える