如何使用FLAG_ONGOING_EVENT将通知转换为正在进行的类型

时间:2012-10-18 07:48:41

标签: android notifications flags

我注意到有两种不同类型的通知出现在Android设备的屏幕顶部。一个是“正在进行”,另一个是“通知”。我想将通知更改为正在进行的类型。一些研究表明我必须设置一个标志:Notification.FLAG_ONGOING_EVENT。我将如何在以下代码中执行此操作,还有什么我需要做的才能将其更改为“正在进行”类型?

     NotificationCompat.Builder mBuilder =
                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 :(得分:4)

Builder有一个“setOngoing”方法。

 NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(AudioService.this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!")
                    .setOngoing(true);