我想创建一个通知视图,就像whatsapp 或短信通知一样。我怎样才能做到这一点。
到目前为止我搜索的内容
面包块:可以在这里找到
这将在运行活动和操作栏下方显示一个油炸面包丁。如果我的活动没有运行或者我正在进行其他活动,这将如何处理。
吐司:这看起来不像吐司。它仅显示在底部或中心。
对话框:可以这样完成,但这会使应用程序模糊。并禁用背景。我不想要这个
任何解决方案将不胜感激。
谢谢
这是来自 android lollipop 的提示通知,您可以在此处查看如何显示通知,您可以在此处查看http://developer.android.com/guide/topics/ui/notifiers/notifications.html ,对于提示,您必须将通知优先级设置为以下
.setPriority(Notification.PRIORITY_HIGH)
希望能帮助到你
编辑 11 月 17 日
Notification.PRIORITY_HIGH在 API 级别 26 中已弃用。请改用(NotificationManager.IMPORTANCE_HIGH)。
可以通过 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));