Android实现系统下拉栏的消息提示——Notification

系统默认样式

默认通知(通用)

效果图

按钮

<Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="notificationDefault"android:text="默认通知(通用)" />

实现

/*** 系统下拉栏默认的通用通知*/
public void notificationDefault(View view) {// 获取NotificationManager管理者对象NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个ActivityPendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);// 获取Notification对象Notification notificationDefault = new Notification();// 设置显示的图标notificationDefault.icon = R.mipmap.ic_launcher;// 设置Title信息notificationDefault.tickerText = "TickerText:您有新短消息,请注意查收!";// 获取当前系统时间notificationDefault.when = System.currentTimeMillis();// 设置显示的信息notificationDefault.setLatestEventInfo(this, "Title信息", "信息内容", pendingIntent);// 设置右下角显示的提示数字notificationDefault.number = 1;// FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除notificationDefault.flags |= Notification.FLAG_AUTO_CANCEL;// 通过通知管理器来发起通知。manager.notify(NOTIFICATION_FLAG, notificationDefault);
}

默认通知(API 11+)

效果图

按钮

<Button
    android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="notificationAPI_11p"android:text="默认通知(API 11+)" />

实现

/*** 系统下拉栏默认的通知(API 11+)*/
public void notificationAPI_11p(View view) {// 获取NotificationManager管理者对象NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个ActivityPendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);// 通过Notification.Builder来创建通知,注意API Level 11之后才支持Notification notificationAPI_11p = new Notification.Builder(this)// 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap icon).setSmallIcon(R.mipmap.ic_launcher)// 设置在status bar上显示的提示文字.setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在下拉status bar后显示的标题.setContentTitle("这里是标题(API 11+)")// 设置在下拉status bar后显示的内容.setContentText("这里是显示的内容")// 关联PendingIntent.setContentIntent(pendingIntent)// 设置在下拉status bar后显示的数字.setNumber(1)// 需要注意build()是在API level 16及之后增加的,在API 11中可以使用getNotificatin()来代替.getNotification();// FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除notificationAPI_11p.flags |= Notification.FLAG_AUTO_CANCEL;// 通过通知管理器来发起通知。manager.notify(NOTIFICATION_FLAG, notificationAPI_11p);
}

默认通知(API 16+)

效果图

按钮

<Button
    android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="notificationAPI_16p"android:text="默认通知(API 16+)" />

实现

/*** 系统下拉栏默认的通知(API 16+)*/
public void notificationAPI_16p(View view) {// 获取NotificationManager管理者对象NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的Activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个ActivityPendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);// 通过Notification.Builder来创建通知,注意API Level 16之后才支持Notification notificationAPI_16p = new Notification.Builder(this)// 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap icon).setSmallIcon(R.mipmap.ic_launcher)// 设置在status bar上显示的提示文字.setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在下拉status bar后显示的标题.setContentTitle("这里是标题(API 16+)")// 设置在下拉status bar后显示的内容.setContentText("这里是显示的内容")// 关联PendingIntent.setContentIntent(pendingIntent)// 设置在下拉status bar后显示的数字.setNumber(1)// 需要注意build()是在API level 16及之后增加的,API11可以使用getNotificatin()来替代.build();// FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。notificationAPI_16p.flags |= Notification.FLAG_AUTO_CANCEL;// 通过通知管理器来发起通知manager.notify(NOTIFICATION_FLAG, notificationAPI_16p);
}

自定义样式

效果图

自定义提示框布局

layout目录下添加my_notification.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FF000000"android:orientation="vertical"><ImageView
        android:id="@+id/icon"android:layout_width="wrap_content"android:layout_height="match_parent" /><TextView
        android:id="@+id/text_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_toRightOf="@+id/icon"android:gravity="center"android:textColor="#FFFFFFFF"android:textSize="20sp" /><ProgressBar
        android:id="@+id/pb"style="?android:attr/progressBarStyleHorizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/text_content"android:layout_gravity="center_vertical"android:layout_toRightOf="@+id/icon" /></RelativeLayout>

按钮

<Button
        android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="myselfNotification"android:text="自定义通知" />

实现

/*** 系统下拉栏自定义的通知*/
public void myselfNotification(View view) {// 获取NotificationManager管理者对象mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// Notification myNotify = new Notification(R.drawable.message,// "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());mNotification = new Notification();// 显示的图片mNotification.icon = R.mipmap.ic_launcher;// 设置在status bar上显示的提示文字mNotification.tickerText = "TickerText:您有新短消息,请注意查收!";// 获取当前系统时间mNotification.when = System.currentTimeMillis();// 表明当通知被用户点击时,通知不自动清除。mNotification.flags = Notification.FLAG_NO_CLEAR;// 加载自定义的布局mRemoteViews = new RemoteViews(getPackageName(), R.layout.my_notification);// 设置图片mRemoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);// 设置文字mRemoteViews.setTextViewText(R.id.text_content, "下载进度");// 设置进度mRemoteViews.setProgressBar(R.id.pb, 100, 10, false);// 设置显示的自定义布局mNotification.contentView = mRemoteViews;// 设置点击通知栏的响应动作Intent intent = new Intent(Intent.ACTION_MAIN);// 设置通知的点击响应PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, 1);mNotification.contentIntent = contentIntent;// 通过通知管理器来发起通知mNotificationManager.notify(NOTIFICATION_FLAG, mNotification);
}

清除指定ID的通知

按钮

<Button
    android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="cleanNotificationById"android:text="清除指定ID的通知" />

实现

/*** 清除指定ID的提示* @param view*/
public void cleanNotificationById(View view){// 获取NotificationManager管理者对象mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 清除id为NOTIFICATION_FLAG的通知mNotificationManager.cancel(NOTIFICATION_FLAG);
}

清除所有通知

按钮

<Button
    android:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="cleanNotificationAll"android:text="清除所有通知" />

实现

/*** 清除所有的提示** @param view*/
public void cleanNotificationAll(View view) {// 获取NotificationManager管理者对象mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// 清除所有的通知mNotificationManager.cancelAll();
}

Android实现系统下拉栏的消息提示——Notification相关推荐

  1. android微信下拉出现小程序,仿新版微信的小程序下拉栏

    原标题:仿新版微信的小程序下拉栏 本项目会对金融交易软件中存在的各种View进行模仿绘制,提供详细的实现思路,收集整理相关算法.文档以及专业资料. https://github.com/scsfwgy ...

  2. android微信下拉出现小程序,Android 仿新版微信的小程序下拉栏

    Android 仿新版微信的小程序下拉栏 上周微信更新到了 6.6.1 版本,加入了微信小游戏.朋友圈都在玩跳一跳.而且现在微信把最近用过的小程序放到了首页顶部,轻轻下拉就可以快速访问了.可以看下效果 ...

  3. ios android 同步的备忘录,iOS 备忘录如何共享给好友编辑 / Android 如何实现下拉搜索 | 有轻功 #012...

    「有轻功」是 AppSo 新栏目 它是「有用功」的迷你瘦身版 每天 10 秒钟 教你 1 个即学即用的手机技巧 微信号 appsolution 后台回复 「有轻功」获取该栏目的所有文章 这是第 12 ...

  4. android自带下拉阻尼动画,android 有阻尼下拉刷新列表的实现方法

    本文将会介绍有阻尼下拉刷新列表的实现,先来看看效果预览: 这是下拉状态: 这是下拉松开手指后listView回滚到刷新状态时的样子: 1. 如何调用 虽然效果图看起来样子不太好看,主要是因为那个蓝色的 ...

  5. android中上拉下滑布局,3年以上勿进!最简单的Android自定义ListView下拉刷新与上拉加载,代码直接拿去用~...

    本文主要针对开发新手,手写实现一个最简单Android自定义listview下拉刷新和上拉加载demo. 不喜可喷,欢迎大佬留言指点. 效果图 一:编写需要在ListView中增加头加载的布局文件,与 ...

  6. android加载时二级联动点击二级联动,Android实现联动下拉框二级地市联动下拉框功能...

    日常使用软件中,为了方便且规范输入,会使用到下拉框进行输入,如注册时生日选项,购物时的地址输入,都会用到下拉框,今日笔者为了巩固已学的知识,实现了二级联动下拉框用作回顾及分享给求知的新手. 思路/步骤 ...

  7. android 下拉刷新实现方式,Android RecyclerView设置下拉刷新的实现方法

    Android RecyclerView设置下拉刷新的实现方法 1 集成 SwipeRefreshLayout 1.1 xml布局文件中使用 android:id="@+id/refresh ...

  8. Office 2016 Excel实现下拉栏

    我对Office并不精通,最多就是拿来码码字,Excel以及PPT用的很少.在这几个工具中,我觉得最有技术含量的就是Excel.特别是在数据处理.各种总结.规划等等方面有着很广泛的使用. 之前有个小功 ...

  9. Android:有关下拉菜单导航的学习(供自己参考)

    Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...

最新文章

  1. Educational Codeforces Round 9 F. Magic Matrix 最小生成树
  2. 目标板挂载NFS方法及错误解决
  3. js动态给当前点击元素添加css类
  4. 看下这段程序 ^_^
  5. 原生Android之(6.0及以上)权限申请
  6. C++palindrome partitioning回文分割算法的实现(附完整源码)
  7. 人脸识别的python实现代码_手把手教你用1行代码实现人脸识别 --Python Face_recognition...
  8. [Oracle] Enable Row Movement
  9. Android—WebView与JS交互
  10. mysql 删除表中 id不等于XXX的 并且XXX字段的重复记录
  11. (原)Lazarus 异构平台下多层架构思路、DataSet转换核心代码
  12. 线性代数:特征向量和特征值
  13. Hive安装与配置详解
  14. 信息熵是怎样炼成的 | 纪念信息论之父香农
  15. 在linux上运行爬虫任务报错:Overridden settings******
  16. 苹果好用的测试软件,四款主流苹果设备管理软件横向评测
  17. ABBYY最新官方免费序列号激活码序列号密钥下载分享
  18. 伺服系统基于陷波滤波器双惯量伺服系统机械谐振抑制matlab Simulink仿真
  19. MPEG4视频编码技术介绍
  20. 如何使用Python画QQ图

热门文章

  1. 中国5G手机的“喋血江湖”
  2. 一维线搜索确定最优步长
  3. wps 图片批量排序,批量使用题注修改图片序号
  4. Excel MODE.MULT函数获取一组数值中出现频率最多的数据
  5. 结合阿里图标实现3/4五角星书写 css
  6. 强力推荐一个完善的物流管理系统
  7. java的map键值类型是否固定_Java中Map根据键值(key)或者值(value)进行排序实现
  8. 电脑是怎样上网的 (四) 局域网与服务器响应
  9. 圆角边框(border-radius属性、border-radius使用规则)
  10. 安装WSL2 Ubuntu时提示指定的网络名不再可用