我想创建一个类似于WhatsApp或短信通知的通知视图。我该如何实现这一点。
到目前为止我所搜寻的内容:
Crouton: 可以在这里找到
这将在运行的活动和操作栏下方显示一个 Crouton。如果我的活动没有运行或者我在其他活动上,该如何处理。
Toast: 这不像一个 Toast。它只会出现在底部或中心位置。
Dialog: 可以这样做,但是这会使应用程序变模糊并禁用背景。而我不想要这个。
任何解决方案都将受到赞赏。
谢谢
我想创建一个类似于WhatsApp或短信通知的通知视图。我该如何实现这一点。
到目前为止我所搜寻的内容:
Crouton: 可以在这里找到
这将在运行的活动和操作栏下方显示一个 Crouton。如果我的活动没有运行或者我在其他活动上,该如何处理。
Toast: 这不像一个 Toast。它只会出现在底部或中心位置。
Dialog: 可以这样做,但是这会使应用程序变模糊并禁用背景。而我不想要这个。
任何解决方案都将受到赞赏。
谢谢
这是一种在 Android Lollipop 及以上版本中使用的悬浮通知。您可以在此处查看如何显示通知:http://developer.android.com/guide/topics/ui/notifiers/notifications.html ,要使用悬浮通知,您需要将通知优先级设置如下:
.setPriority(Notification.PRIORITY_HIGH)
希望它有所帮助
编辑于Nov-17
Notification.PRIORITY_HIGH在API level 26中已被弃用,请使用(NotificationManager.IMPORTANCE_HIGH)代替。
Notification.PRIORITY_MAX
已经被弃用。 - pRaNaYNotificationCompat.Builder
时,使用NotificationCompat.PRIORITY_HIGH
或NotificationCompat.PRIORITY_MAX
代替。 - pRaNaY可以通过 Headsup 通知来完成。这是 Andorid L 的一个功能,下面是演示代码:
Notification createNotification(boolean makeHeadsUpNotification) {
Notification.Builder notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContentTitle("Sample Notification")
.setContentText("This is a normal notification.");
if(Build.VERSION.SDK_INT> Build.VERSION_CODES.KITKAT)
{
notificationBuilder.setCategory(Notification.CATEGORY_MESSAGE);
}
if (makeHeadsUpNotification) {
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(this, IndexActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
push, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder
.setContentText("Heads-Up Notification on Android L or above.")
.setFullScreenIntent(fullScreenPendingIntent, true);
}
return notificationBuilder.build();
}
您可以这样调用它
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context
.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, createNotification(
true));