`
libo19881179
  • 浏览: 266543 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Notification实现下载进度显示!

阅读更多

用惯了Android的人在刚拿到iPhone的时候,总是会习惯性的用手指从状态栏往下拖一下,这都是给Notification闹的。
不过Notification也确实是1个不错的提示工具,不干扰正常的操作,事后还可以再翻看详细的内容,点击后还可以进入相关的画面查看更具体的内容。
今天我就以代码为主的形式来介绍Notification的使用,包括基本用法,自定义的View,以及更多的控制方法。
另一种Android中常用到的提示方法Toast的用法请参见《教程:在Android中使用Toast进行提示》
我们先看下Notification的几个主要组成部分:
Icon:不解释
Ticker Text:Notification刚出来的时候,在状态栏上滚动的字幕,如果很长,会自动分割滚动
Icon和Ticker Text
Content Title:Notification展开后的标题
Content Text:Notification展开后的内容
Content Title和Text

 

Notification的一般用法

取得NotificationManager

1
2
3
private NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager) 
	getSystemService(Context.NOTIFICATION_SERVICE);

创建Notification并且显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Notification的滚动提示
String tickerText = "My notification, It's a long text! Hello World desiyo?";
//Notification的图标,一般不要用彩色的
int icon = R.drawable.icon_02241_3;
 
//contentTitle和contentText都是标准的Notification View的内容
//Notification的内容标题,拖下来后看到的标题
String contentTitle="My notification";
//Notification的内容
String contentText="Hello World!";
 
//Notification的Intent,即点击后转向的Activity
Intent notificationIntent = new Intent(this, this.getClass());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
	notificationIntent, 0);
 
//创建Notifcation
Notification notification = new Notification(icon, tickerText, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification.defaults |= Notification.DEFAULT_SOUND;
//设定如何振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification.flags|=Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
//显示这个notification
mNotificationManager.notify(HELLO_ID, notification);

这是最基本的应用,可以说除了找个合适的图标以外,其它都很简单。

使用自定义View的Notification

同Toast一样,我们也可以自已指定1个View来作为Notification展开后的显示内容,比如说在Android Market中下载的时候,Notification中会显示当前下载的进度,那么我们也来模拟1个这样的效果吧。
首先给出View的定义文件:notification_view_sample.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:padding="3dp"
>
	<ImageView android:id="@+id/notificationImage"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:src="@android:drawable/stat_sys_download"
	/>
	<TextView android:id="@+id/notificationTitle"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_toRightOf="@id/notificationImage"
		android:layout_alignParentRight="true"
		android:paddingLeft="6dp"
		android:textColor="#FF000000"
	/>
	<TextView android:id="@+id/notificationPercent"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_below="@id/notificationImage"
		android:paddingTop="2dp"
		android:textColor="#FF000000"
	/>
	<ProgressBar android:id="@+id/notificationProgress"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_below="@id/notificationTitle"
		android:layout_alignLeft="@id/notificationTitle"
		android:layout_alignParentRight="true"
		android:layout_alignTop="@id/notificationPercent"
		android:paddingLeft="6dp"
		android:paddingRight="3dp"
		android:paddingTop="2dp"
		style="?android:attr/progressBarStyleHorizontal"
	/>
</RelativeLayout>

RelativeLayout的使用,可以参考:《教程:Android各种Layout特性和使用汇总(一)》

接下来是Java代码片段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Notification的滚动提示
String tickerText1 = "Custom view for download notification";
//Notification的图标,一般不要用彩色的
int icon1 = android.R.drawable.stat_sys_download;
 
//Notification的Intent,即点击后转向的Activity
Intent notificationIntent1 = new Intent(this, this.getClass());
notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0, notificationIntent1, 0);
 
//创建Notifcation
Notification notification1 = new Notification(icon1, tickerText1, System.currentTimeMillis());
//设定Notification出现时的声音,一般不建议自定义
notification1.defaults |= Notification.DEFAULT_SOUND;
//设定是否振动
notification1.defaults |= Notification.DEFAULT_VIBRATE;
//notification.number=numbers++;
//指定Flag,Notification.FLAG_AUTO_CANCEL意指点击这个Notification后,立刻取消自身
//这符合一般的Notification的运作规范
notification1.flags|=Notification.FLAG_ONGOING_EVENT;
 
//创建RemoteViews用在Notification中
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_view_sample);
contentView.setTextViewText(R.id.notificationTitle, "Download:Facebook for android");
contentView.setTextViewText(R.id.notificationPercent, "35%");
contentView.setProgressBar(R.id.notificationProgress, 100, 35, false);
 
notification1.contentView = contentView;
notification1.contentIntent=contentIntent1;
 
//显示这个notification
mNotificationManager.notify(CUSTOM_VIEW_ID, notification1);

注意以上代码中使用的是RemoteViews,而不是普通的View,另外使用的是PendingIntent而不是普通的Intent,这都说明了Notification是1个“远程”的东西,其中能够使用的控件是受限制的,比如说TableLayout就不能使用。看下效果图,是不是和Market中的界面很接近呢?
自定义View的Notification效果图

更好的控制Notification

动画图标怎么做?

和selector类似,定义1个XML文件放在drawable下,下面是之前用到的stat_sys_download的定义:

1
2
3
4
5
6
7
8
9
10
<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    <item android:drawable="@drawable/stat_sys_download_anim0" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim1" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim2" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim3" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim4" android:duration="200" />
    <item android:drawable="@drawable/stat_sys_download_anim5" android:duration="200" />
</animation-list>
如何更新Notification?

注意到前面的代码中用到的CUSTOM_VIEW_ID,这是Notification的ID,如果2次弹出的Notification的ID相同,那么Notification就只会更新而不会再次滚动提醒。之前给出的ProgressBar是不会动的,利用这个方法就可以让它动起来(或者也可以直接调用RemoteView的set方法来直接更新?未试验)

如何自定义提示的声音和振动?
//自定义提示音
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
//自定义振动方式
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

请注意:如果使用了DEFAULT_SOUND或DEFAULT_VIBRATE,则自定义的提示音和振动无效。

在类似于短消息的应用中如何提示数量?

使用Notification的number属性,默认为0,如果是1或更大的数字,则会在图标上覆盖显示这个数字。
notification.number=notificationNumber;

Flag的使用

notification有1个flag属性,除了DEFAULT_SOUND之外,还有几个很有用的属性。
FLAG_AUTO_CANCEL:自动清除Notification,前面的例子中有用到
FLAG_INSISTENT:提示音一直不停,直至用户响应(很吵吧!)
FLAG_ONGOING_EVENT:表示这是1个正在进行的任务,不可以清除,第2个例子中有用到
FLAG_NO_CLEAR:不可以清除

© 2011, Bing. 版权所有。 所有转载请以链接方式进行。

 

分享到:
评论

相关推荐

    Android实现Service下载文件,Notification显示下载进度的示例

    本篇文章主要介绍了Android实现Service下载文件,Notification显示下载进度,具有一定的参考价值,有兴趣的可以了解一下。

    详解Android中使用Notification实现进度通知栏(示例三)

    我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能。实现效果如下: 在...

    AsyncTask文件下载控制暂停和继续,在状态栏中显示进度条进度

    使用AsyncTask实现文件下载,可以控制暂停和继续,并且在状态栏中显示下载的进度

    UpdateManager

    很多软件都有自动更新的功能,该类实现了更新功能,在notification栏显示下载进度,下载完后可点击安装。

    Android-AppUpdate:Android App 在线升级

    Android-AppUpdateAndroid App 在线升级通过json解析判断升级版本使用DownloadManager实现下载后台自动下载,Notification显示下载进度可以自定义下载路径和下载文件名称下载后自动安装提示

    SilentUpdate::right_arrow_curving_up:基于Kotlin的静默更新应用库 A library silently & automatically download latest apk to update your App

    下载时,是静默状态,不会有通知栏显示进度 下载完成,接收回调(onDownLoadSuccess),显示Notification和Dialog 用户点击DownloadSuccessDialog或Notification即跳转到安装界面 二:流量的情况【用户自行操作】 显示...

    android开发demo集合

    45、下载状态栏显示下载进度 46、Gallery3d效果 47、ListView 上拉加载更多效果 48、异步加载图片的二级缓存技术 49、QQ的好友列表展示效果 50、Fragment + ViewPager实现tab滑动切换 51、能够显示在桌面前面的...

    android初学者入门项目

    45、下载状态栏显示下载进度 46、Gallery3d效果 47、ListView 上拉加载更多效果 48、异步加载图片的二级缓存技术 49、QQ的好友列表展示效果 50、Fragment + ViewPager实现tab滑动切换 51、能够显示在桌面前面的...

    MFC制作的MP3

    //显示时间进度 dc.TextOut(280,128,mtime); Mp3 mp3; mp3.Load(this-&gt;m_hWnd,strfilepath); GetDlgItem(IDC_open)-&gt;EnableWindow(TRUE); GetDlgItem(IDC_pause)-&gt;EnableWindow(TRUE); GetDlgItem(IDC_stop)-&gt;...

    assignment

    底部进度栏未实现,没有逻辑 在首次加载时,如果没有设置每日目标,则必须手动按DailyGoal按钮,我懒于实现总线以从Bloc调用它,并且不想实现从小部件本身调用它的不良解决方案 :female_sign:‍:female_sign: 演示...

    Android典型技术模块开发详解

    8.11.2 带进度的Notification 8.12 Toast(提示) 8.13 本章小结 第9章 风格与动画 9.1 主题与风格 9.1.1 风格style 9.1.2 主题Theme 9.2 View的绘制 9.3 Tween Animation 9.3.1 Alpha动画 9.3.2 Scale动画 9.3.3 ...

    Google Android SDK开发范例大全(第3版)part2

     5.29 模拟文件下载Notification进度变化与关闭  5.30 取得已安装应用程序列表与安装日期信息  5.31 造假的Toast画面  5.32 剪贴簿管理器  第6章 手机自动服务纪实  6.1 您有一条短信pop up提醒  6.2 ...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用ContentResolver 5.10 取得联系人资料——Provider.Contact的使用 5.11 制作有图标的文件资源管理...

    andriod精华学习教程

    Notification Manager来实现。 一般来说,在应用程序里,100到200ms是用户能感知阻滞的时间阈值。因此,这 里有一些额外的技巧来避免ANR,并有助于让你的应用程序看起来有响应性。 如果你的应用程序为响应用户输入...

    《Google Android SDK开发范例大全(第3版)》.pdf

    5.29 模拟文件下载notification进度变化与关闭 285 5.30 取得已安装应用程序列表与安装日期信息 291 5.31 造假的toast画面 294 5.32 剪贴簿管理器 298 第6章 手机自动服务纪实 302 6.1 您有一条短信...

    Google Android SDK开发范例大全(第3版) 1/5

    系统服务及研发的整合:网络搜索、联系人、音乐、应用程序、定制手机文件管理、记忆卡I/O存取、双向短信、闹钟服务、开机程序、来电通信互动、拜年短信、信息提醒、电池电量显示、进度显示、取得应用程序信息等。...

Global site tag (gtag.js) - Google Analytics