154

Android でフォアグラウンド サービスをセットアップしています。通知テキストを更新したいと思います。以下のようなサービスを作成しています。

このフォアグラウンド サービス内で設定されている通知テキストを更新するにはどうすればよいですか? 通知を更新するためのベスト プラクティスは何ですか? サンプルコードをいただければ幸いです。

public class NotificationService extends Service {

    private static final int ONGOING_NOTIFICATION = 1;

    private Notification notification;

    @Override
    public void onCreate() {
        super.onCreate();

        this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, AbList.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);

        startForeground(ONGOING_NOTIFICATION, this.notification);

    }

以下に示すように、メインのアクティビティでサービスを作成しています。

    // Start Notification Service
    Intent serviceIntent = new Intent(this, NotificationService.class);
    startService(serviceIntent);
4

5 に答える 5

258

startForeground() によって設定された通知を更新する場合は、単に新しい通知を作成し、それを通知するために NotificationManager を使用します。

重要な点は、同じ通知 ID を使用することです。

通知を更新するために startForeground() を繰り返し呼び出すシナリオはテストしませんでしたが、 NotificationManager.notify を使用する方がよいと思います。

通知を更新しても、サービスがフォアグラウンド ステータスから削除されることはありません (これは、 stopForground を呼び出すことによってのみ実行できます)。

例:

private static final int NOTIF_ID=1;

@Override
public void onCreate (){
    this.startForeground();
}

private void startForeground() {
    startForeground(NOTIF_ID, getMyActivityNotification(""));
}

private Notification getMyActivityNotification(String text){
    // The PendingIntent to launch our activity if the user selects
    // this notification
    CharSequence title = getText(R.string.title_activity);
    PendingIntent contentIntent = PendingIntent.getActivity(this,
            0, new Intent(this, MyActivity.class), 0);

    return new Notification.Builder(this)
            .setContentTitle(title)
            .setContentText(text)
            .setSmallIcon(R.drawable.ic_launcher_b3)
            .setContentIntent(contentIntent).getNotification();     
}

/**
 * This is the method that can be called to update the Notification
 */
private void updateNotification() {
    String text = "Some text that will update the notification";

    Notification notification = getMyActivityNotification(text);

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIF_ID, notification);
}

ドキュメントの状態

更新できるように通知を設定するには、 を呼び出して通知 ID を指定して通知を発行しますNotificationManager.notify()。発行後にこの通知を更新するには、オブジェクトを更新または作成し、 そこからNotificationCompat.Builderオブジェクトを構築し、以前に使用したのと同じ ID でNotificationを発行します。Notification以前の通知がまだ表示されている場合、システムはNotificationオブジェクトの内容から通知を更新します。以前の通知が却下された場合は、代わりに新しい通知が作成されます。

于 2013-11-22T10:31:10.627 に答える
67

このシナリオは試していませんがstartForeground()、同じ一意の ID と新しい情報を使用して再度呼び出すとうまくいくと思います。Notification

更新: コメントに基づいて、NotifcationManager を使用して通知を更新する必要があり、サービスは引き続きフォアグラウンド モードのままになります。以下の答えを見てください。

于 2011-04-03T11:52:49.110 に答える
6

サービスでこれを行うコードは次のとおりです。新しい通知を作成しますが、startForeground で使用したのと同じ通知 ID を通知するよう通知マネージャーに依頼します。

Notification notify = createNotification();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext()
    .getSystemService(getApplicationContext().NOTIFICATION_SERVICE);

notificationManager.notify(ONGOING_NOTIFICATION, notify);

完全なサンプル コードについては、ここで確認できます。

https://github.com/plateaukao/AutoScreenOnOff/blob/master/src/com/danielkao/autoscreenonoff/SensorMonitorService.java

于 2013-06-06T16:53:45.607 に答える