0

我知道有一种有用的方法 setOngoing(true) 可以将通知设置为正在进行的类型。但是,是否有可能以另一种方式做到这一点?喜欢使用 FLAG_ONGOING_EVENT?例如,我如何使用它来将下面的通知代码更改为正在进行的类型?

     new NotificationCompat.Builder(AudioService.this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My notification")
            .setContentText("Hello World!");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(AudioService.this, MainActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(AudioService.this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                0,
                PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
4

1 回答 1

3

您可以直接在通知上设置标志。为此,请从.build()方法中获取结果并使用该flags字段。

这是一个将“正在进行的事件”标志添加到任何现有标志的示例:

Notification note = mBuilder.build();
note.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(mId, note);

它应该相当于调用mBuilder.setOngoing(true).

于 2012-10-19T09:33:34.703 回答