状态栏上显示单行通知?

时间:2012-12-22 07:27:14

标签: android notifications

只是一个简单的问题:如何使单线通知在短时间后消失。

就像Whatsapp的:

Whatsapp notification

我当前的通知代码仍然非常基础:

Intent intent = new Intent("com.example.MyChat");
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new Notification.Builder(context)
        .setContentTitle("New message from " + name)
        .setContentText(message)
        .setSmallIcon(R.drawable.ic_launcher).setContentIntent(pIntent)
        .getNotification();

notification.defaults |= Notification.DEFAULT_SOUND;

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);

由于

2 个答案:

答案 0 :(得分:1)

您可以在短时间后(例如5秒)使用AlarmManager来调用。

此外,您可以在首次出现通知时使用启动的服务,并在一段时间后取消通知。

在这里你可以使用

取消通知
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTFICATION_ID);

看一下这个post,它也可以帮到你。

答案 1 :(得分:1)

这称为“自动收报机”,如果您设置了通知的tickerText属性,则状态栏会简要显示。

请参阅setTickerText