手机通知栏
来源:互联网 发布:淘宝售前客服怎么做 编辑:程序博客网 时间:2024/06/05 20:18
手机通知栏主要设计的类:
Notification、NotificationManager、Intent、PendingIntent、TaskStackBuilder Action、RemoteViews
1、Notification类没有直接使用,通知栏与弹窗使用了相同的设计模式------工厂模式
new Notification.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Notification")
.setContentText("hello world");
通过使用Notification.Builder中的build方法生成一个Notification。
2、Intent负责使用在点击时调用指定的Activity
在Notification中只能设定PendingIntent,需要根据TaskStackBuilder构建一个任务栈,并向任务栈中添加下一个任务的Intent
通过使用TaskStackBuiler来获取一个PendingIntent,
Intent resultTintent = new Intent(this,MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultTintent);
PendingIntent sendIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
3、NotificationManager负责Notification的添加和移除
在添加Notification时需要制定当前添加Notification的Id,重复添加相同Id的Notification时,如果原有的Notification没有被销毁,
将不再创建新的Notification,只是更新Notification的内容。
个人感觉:对于NotificationManager而言,使用Notification的Id表示是否是同一个Notification。
NotificationManager没有提供获取Notification的Id的方法,但是可以获取当前活动的Notification的数组,通过对数组进行遍历可以获取
指定的Notification。
mManager.notify(99,mBuilder.build());
mManager.cancel(99);
4、RemoteViews用于自定义Notification的布局。
通过使用Notification.Builder的setContentView方法添加自定义的布局。
在使用RemoteViews进行自定义布局时,需要另外指定Notification的smallIcon,不然会报错。
自定义通知布局的可用高度取决于通知视图。普通视图布局限制为 64 dp,扩展视图布局限制为 256 dp。
要定义自定义通知布局,请首先实例化 RemoteViews 对象来扩充 XML 布局文件。然后,调用 setContent(),而不是调用 setContentTitle() 等方法。要在自定义通知中设置内容详细信息,请使用 RemoteViews 中的方法设置视图子项的值:
1. 在单独的文件中为通知创建 XML 布局。您可以根据需要使用任何文件名,但必须使用扩展名 .xml。
2. 在您的应用中,使用 RemoteViews 方法定义通知的图标和文本。通过调用 setContent() 将此 RemoteViews 对象放入 NotificationCompat.Builder中。
5、设置Notification的可见性
调用 setVisibility() 并指定以下值之一:
- VISIBILITY_PUBLIC 显示通知的完整内容。
- VISIBILITY_SECRET 不会在锁定屏幕上显示此通知的任何部分。
- VISIBILITY_PRIVATE 显示通知图标和内容标题等基本信息,但是隐藏通知的完整内容。
设置 VISIBILITY_PRIVATE 后,您还可以提供其中隐藏了某些详细信息的替换版本通知内容。例如,短信 应用可能会显示一条通知,指出“您有 3 条新短信”,但是隐藏了短信内容和发件人。要提供此替换版本的通知,请先使用 NotificationCompat.Builder 创建替换通知。创建专用通知对象时,请通过setPublicVersion() 方法为其附加替换通知。
6、在Notification中显示进度条,显示音乐播放控制控件:
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder =newNotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
newThread(
newRunnable(){
@Override
publicvoid run(){
int incr;
// Do the "lengthy" operation 20 times
for(incr =0; incr <=100; incr+=5){
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
mBuilder.setProgress(100, incr,false);
// Displays the progress bar for the first time.
mNotifyManager.notify(0, mBuilder.build());
// Sleeps the thread, simulating an operation
// that takes time
try{
// Sleep for 5 seconds
Thread.sleep(5*1000);
}catch(InterruptedException e){
Log.d(TAG,"sleep failure");
}
}
// When the loop is finished, updates the notification
mBuilder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotifyManager.notify(ID, mBuilder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
setProgress(0,0,false);相当于清除进度条setProgress(100,i,false);--- i != 0时当设置为true时,之前绘制的进度条会被清除,设置为false则不会清除。
setProgress(0,0,true);生成一个不断循环滚动的进度条
0 0
- 手机通知栏
- 往手机通知栏发送一条通知
- android 手机收起通知栏
- Android-Notification安卓手机通知栏通知的开发
- 某些手机上点击通知栏的通知,无反应。
- 手机应用通知栏的创造
- 四种通知栏提醒,Android实现通知栏提醒方法,手机通知栏适配测试Demo
- 手机电池电量剩余通知
- 【小技巧】Android手机通知栏广告去除方法
- android手机通知栏恶意广告!简单有效!
- 让你监控Android手机的通知栏
- 修改标题栏和手机通知栏颜色相同
- Android之手机通知栏Notification介绍(一)
- Android之手机通知栏Notification的使用(二)
- Unity类似手机通知栏的滑动效果
- Delphi 监听安卓(Android)手机通知栏信息
- android手机root后的安全问题 (二)获取通知栏通知
- 安卓4.3系统,请问如何关闭通知栏里金山手机毒霸的常驻通知
- Linux虚拟内存
- Android Studio 解决 com.android.application 找不到
- #88 Lowest Common Ancestor
- 从零开始学习jQuery (八) 插播:jQuery实施方案
- SpringMVC4.0以后版本返回json格式数据问题
- 手机通知栏
- 树莓派驱动6轴传感器MPU6500与SPI初体验(一)
- JAVA_WEB设计模式3
- Activity的生命周期及传值
- git 上传项目到github
- 从零开始学习jQuery (九) jQuery工具函数
- Hadoop下Mahout配置
- ios的手势操作之UIGestureRecognizer浅析
- 从零开始学习jQuery (十) jQueryUI常用功能实战