如何在通知中添加标志(状态栏通知)?

时间:2010-10-19 23:38:59

标签: android notifications

我遵循开发者指南中的教程,但我遇到了问题...

教程说:“要在用户从”通知“窗口中选择状态栏通知时清除状态栏通知,请将”FLAG_AUTO_CANCEL“标志添加到”通知“对象”

但是...我如何将标志添加到我的通知中?

通知没有任何类型的功能来添加标志....然后呢?我怎么能这样做?

3 个答案:

答案 0 :(得分:16)

通知标志是公共成员字段。

Notification notif = new Notification(R.drawable.icon, shortMsg,
                System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;

答案 1 :(得分:2)

当您定义Intent时,标志会结束,如下所示:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, Notification.FLAG_AUTO_CANCEL);

答案 2 :(得分:0)

下面是您可以尝试使用的代码,它可以正常工作。

public void notification(Context context,String title, String text) {
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(context)
        .setAutoCancel(true)
        .setSound(alarmSound)
        .setSmallIcon(R.drawable.appicon)
        .setContentTitle(title)
        .setContentText(text);

    Intent notificationIntent = new Intent(this,gps_get_data.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,                                           PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
}