Android实现系统下拉栏的消息提示——Notification
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跳转启动到哪个Activity
PendingIntent 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跳转启动到哪个Activity
PendingIntent 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跳转启动到哪个Activity
PendingIntent 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的更多相关文章
- Android 第三方开源下拉框:NiceSpinner
Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框Spinner所提供的设计样式,而改用自定 ...
- Android:有关下拉菜单导航的学习(供自己参考)
Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...
- Android 禁止状态栏下拉
同学项目用到Android 禁止状态栏下拉,我也迷茫,网上很多资料都不行,最终找到了下面一篇博客,感觉很不错,说的比较详细,供大家参考了 http://blog.csdn.net/u011913612 ...
- android 开发-spinner下拉框控件的实现
Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...
- Android第三方开源下拉框:NiceSpinner
Android第三方开源下拉框:NiceSpinner Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Andro ...
- 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)
1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...
- 解决bootStrap selectpicker 下拉栏上方弹出
最近项目中遇到了一个使用bootStrap selectpicker 进行下拉栏展示的时候出现在元素上方弹出展示的问题,可把我难受坏了,和测试互怼最终以失败告终(人家还是一个娇滴滴的小姑娘),在查了a ...
- 第13讲- Android之消息提示Notification
第13讲 Android之消息提示Notification .Notification Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status ...
- jQuery 下拉框输入匹配提示选项
做页面输入时,为方便输入和提高用户体验,常用下拉框,当下拉选项数据很多时,也不易找到想要的选项,这时,提供一种下拉框输入匹配提示选项,如下: 图示
随机推荐
- 各种电脑进入BIOS快捷键
组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台 ...
- [SHOI 2008]Debt 循环的债务
Description 题库链接 A 欠 B \(x_1\) 元, B 欠 C \(x_2\) 元, C 欠 A \(x_3\) 元.现每人手上各有若干张 100,50,20,10,5,1 钞票.问至 ...
- [CQOI 2015]选数
Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公 ...
- [BZOJ 2169]连边
Description 有N个点(编号1到N)组成的无向图,已经为你连了M条边.请你再连K条边,使得所有的点的度数都是偶数.求有多少种连的方法.要求你连的K条边中不能有重边,但和已经连好的边可以重.不 ...
- [AHOI2012]铁盘整理
题目描述 输入输出格式 输入格式: 共两行.第一行为铁盘个数N(1<=N<=50),第二行为N个不同的正整数,分别为从上到下的铁盘的半径R.(1<=R<=100) 输出格式: ...
- ●Joyoi Normal
题链: http://www.joyoi.cn/problem/tyvj-1953题解: 定义d(u,v)这个函数,满足: d(u,v)=1,当且仅当在点分树中,u是v的祖先 d(u,v)=0,其它情 ...
- [HNOI2015]实验比较
Description 小D 被邀请到实验室,做一个跟图片质量评价相关的主观实验.实验用到的图片集一共有 N 张图片,编号为 1 到 N.实验分若干轮进行,在每轮实验中,小 D会被要求观看某两张随机选 ...
- [UOJ UNR #2]积劳成疾
来自FallDream的博客,未经允许,请勿转载,谢谢. 传送门 区间最大值的题emmmm 想到构建笛卡尔树,这样自然就想到了一种dp f[i][j]表示大小为i的笛卡尔树,根的权值是j的答案. 转移 ...
- NOIP2016 玩脱记
NOIP前: NOIP前停课了一个多月,这一个多月里浪得飞起,内心十分紧张,然后就不知不觉就到NOIP了. Day 0: 上火车前ryc给我们出了道题"一个数列,只有两个数出现了奇数次,找出 ...
- SqlServer 跨网段跨服务器复制
注意:被同步的表必须有主键,否则无法同步.对数据库进行操作时需要登录服务器,在服务器本地进行操作,远程对数据库进行操作不能完成所有的步骤 准备工作: 1.将发布数据库完整备份到订阅服务器上,并在订阅服 ...