通过使用FLAG_ONGOING_EVENT替代setOngoing(true)方法将通知类型设置为进行中

时间:2012-10-19 07:35:20

标签: android notifications android-notifications

我知道有一个不同的有用方法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());

1 个答案:

答案 0 :(得分:3)

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

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

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

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