Android系列教程(1):Notification 与状态栏信息
2010-07-16 10:31
417 查看
[b][b][b][b][b][b][b]本文为《Android/OPhone 开发完全讲义》的内容连载,如需转载,请注明作者和出处,谢谢!
[/b]
[/b]
[/b]
[/b]
[/b]
[/b]
[/b]
源代码下载
Notification
与
Toast
都可以起到通知、提醒的作用。但它们的实现原理和表现形式却完全不一样。
Toast
其实相当于一个组件(
Widget
)。有些类似于没有按钮的对话
框。而
Notification
是显示在屏幕上方状态栏中的信息。还有就是
Notification
需要用
NotificationManager
来管理,而
Toast
只需要简单地创建
Toast
对象即可。
下面来看一下创建并显示一个
Notification
的步骤。创建和显
示一个
Notification
需要如下
5
步:
1.
通过
getSystemService
方法获得一个
NotificationManager
对象。
2.
创建一个
Notification
对象。每一个
Notification
对应一个
Notification
对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源
ID
和发出通知的时间。一般为当前时间。
3.
由于
Notification
可以与应用程序脱离。也就是说,即使应用程序被关闭,
Notification
仍然会显示在状态栏
中。当应用程序再次启动后,又可以重新控制这些
Notification
。如清除或替换它们。因此,需要创建一个
PendingIntent
对象。该对象由
Android
系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。
4.
使用
Notification
类的
setLatestEventInfo
方法设置
Notification
的详细信息。
5.
使用
NotificationManager
类的
notify
方法显示
Notification
消息。在这一步需要指定标识
Notification
的唯一
ID
。这个
ID
必须相对于同一个
NotificationManager
对象是唯一的,否则就会覆盖相同
ID
的
Notificaiton
。
心动不如行动,下面我们来演练一下如何在状
态栏显示一个
Notification
,代码如下:
上面的
5
行代码正好对应创建和显示
Notification
的
5
步。在这里要解释一下的是
notify
方法的第
1
个参数。这个参数实际上表示了
Notification
的
ID
。是一个
int
类型的值。为了使这个值唯一,可以使用
res
目录中的某些资源
ID
。例如,在上面的代码中使用了当前
Notification
显示的图像对应的
资源
ID
(
R.drawable.icon
)作为
Notification
的
ID
。当然,读者也可以使用其他的值作为
Notification
的
ID
值。
由于创建和显示多个
Notification
的代码类似,因
此,在本节的例子中编写了一个
showNotification
方法来显示
Notification
,代码如下:
下面的代码使用
showNotification
方法显示了
3
个
Notification
消息。
其中第
2
个和第
3
个
Notification
使用的是同一个
ID
(
R.drawabgle.why
),因此,第
3
个
Notification
会覆盖第
2
个
Notification
。
在显示
Notification
时还可以设置显示通
知时的默认发声、震动和
Light
效果。要实现这个功能需要设置
Notification
类的
defaults
属性,代码如下:
注意:设置默认发声、震动和
Light
的方法是
setDefaults
。该
方法与
showNotification
方法的实现代码基本相同,只是在调用
notify
方法之前需要设置
defaults
属性(
defaults
属性必须在
调用
notify
方法之前调用,否则不起作用)。在设置默认震动效果时还需要在
AndroidManifest.xml
文件中通过
<uses-permission>
标签设置
android.permission.VIBRATE
权限。
如果要清除某个消息,可以使用
NotificationManager
类
的
cancel
方
法,该方法只有一个参数,表示要清除的
Notification
的
ID
。使用
cancelAll
可以清除当前
NotificationManager
对象中的所有
Notification
。
运行本节的例子,单击屏幕上显示
Notification
的按钮,会显示如
图1
所示的消息。每一个消息会显示一会,然后就只显示整个
Android
系统(也包括其他应用程序)
的
Notification
(只显示图像部分)。如图2
所示。如果将状态栏拖下来,可以看
到
Notification
的详细信息和发出通知的时间(也就是
Notification
类的构造方法的第
3
个参数值),如图3
所
示。当单击【清除通知】按钮,会清除本应用程序显示的所有
Notification
,清除后的效果如图4
所示。
图1
图2
图3
图4
[/b]
[/b]
[/b]
[/b]
[/b]
[/b]
[/b]
源代码下载
Notification
与
Toast
都可以起到通知、提醒的作用。但它们的实现原理和表现形式却完全不一样。
Toast
其实相当于一个组件(
Widget
)。有些类似于没有按钮的对话
框。而
Notification
是显示在屏幕上方状态栏中的信息。还有就是
Notification
需要用
NotificationManager
来管理,而
Toast
只需要简单地创建
Toast
对象即可。
下面来看一下创建并显示一个
Notification
的步骤。创建和显
示一个
Notification
需要如下
5
步:
1.
通过
getSystemService
方法获得一个
NotificationManager
对象。
2.
创建一个
Notification
对象。每一个
Notification
对应一个
Notification
对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源
ID
和发出通知的时间。一般为当前时间。
3.
由于
Notification
可以与应用程序脱离。也就是说,即使应用程序被关闭,
Notification
仍然会显示在状态栏
中。当应用程序再次启动后,又可以重新控制这些
Notification
。如清除或替换它们。因此,需要创建一个
PendingIntent
对象。该对象由
Android
系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。
4.
使用
Notification
类的
setLatestEventInfo
方法设置
Notification
的详细信息。
5.
使用
NotificationManager
类的
notify
方法显示
Notification
消息。在这一步需要指定标识
Notification
的唯一
ID
。这个
ID
必须相对于同一个
NotificationManager
对象是唯一的,否则就会覆盖相同
ID
的
Notificaiton
。
心动不如行动,下面我们来演练一下如何在状
态栏显示一个
Notification
,代码如下:
上面的
5
行代码正好对应创建和显示
Notification
的
5
步。在这里要解释一下的是
notify
方法的第
1
个参数。这个参数实际上表示了
Notification
的
ID
。是一个
int
类型的值。为了使这个值唯一,可以使用
res
目录中的某些资源
ID
。例如,在上面的代码中使用了当前
Notification
显示的图像对应的
资源
ID
(
R.drawable.icon
)作为
Notification
的
ID
。当然,读者也可以使用其他的值作为
Notification
的
ID
值。
由于创建和显示多个
Notification
的代码类似,因
此,在本节的例子中编写了一个
showNotification
方法来显示
Notification
,代码如下:
private void showNotification(String tickerText, String contentTitle, String contentText, int id, int resId) { Notification notification = notification = new Notification(resId, tickerText, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); // notificationManager是在类中定义的 NotificationManager变量。在onCreate方法中已经创建 notificationManager.notify(id, notification); }
下面的代码使用
showNotification
方法显示了
3
个
Notification
消息。
showNotification("今天非常高兴", "今天考试得了全年级第一", "数学100分、语文99分、英语100分,yeah!", R.drawable.smile, R.drawable.smile); showNotification("这是为什么呢?", "这道题为什么会出错呢?", "谁有正确答案啊.", R.drawable.why, R.drawable.why); showNotification("今天心情不好", "也不知道为什么,这几天一直很郁闷.", "也许应该去公园散心了", R.drawable.why, R.drawable.wrath);
其中第
2
个和第
3
个
Notification
使用的是同一个
ID
(
R.drawabgle.why
),因此,第
3
个
Notification
会覆盖第
2
个
Notification
。
在显示
Notification
时还可以设置显示通
知时的默认发声、震动和
Light
效果。要实现这个功能需要设置
Notification
类的
defaults
属性,代码如下:
notification.defaults = Notification.DEFAULT_SOUND; // 使用默认的声音 notification.defaults = Notification.DEFAULT_VIBRATE; // 使用默认的震动 notification.defaults = Notification.DEFAULT_LIGHTS; // 使用默认的Light notification.defaults = Notification.DEFAULT_ALL; // 所有的都使用默认值
注意:设置默认发声、震动和
Light
的方法是
setDefaults
。该
方法与
showNotification
方法的实现代码基本相同,只是在调用
notify
方法之前需要设置
defaults
属性(
defaults
属性必须在
调用
notify
方法之前调用,否则不起作用)。在设置默认震动效果时还需要在
AndroidManifest.xml
文件中通过
<uses-permission>
标签设置
android.permission.VIBRATE
权限。
如果要清除某个消息,可以使用
NotificationManager
类
的
cancel
方
法,该方法只有一个参数,表示要清除的
Notification
的
ID
。使用
cancelAll
可以清除当前
NotificationManager
对象中的所有
Notification
。
运行本节的例子,单击屏幕上显示
Notification
的按钮,会显示如
图1
所示的消息。每一个消息会显示一会,然后就只显示整个
Android
系统(也包括其他应用程序)
的
Notification
(只显示图像部分)。如图2
所示。如果将状态栏拖下来,可以看
到
Notification
的详细信息和发出通知的时间(也就是
Notification
类的构造方法的第
3
个参数值),如图3
所
示。当单击【清除通知】按钮,会清除本应用程序显示的所有
Notification
,清除后的效果如图4
所示。
图1
图2
图3
图4
相关文章推荐
- Android系列讲座(1):Notification 与状态栏信息
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!
- Android 中 Notification 与状态栏信息
- Android基础入门教程——2.5.2 Notification(状态栏通知)详解
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用! .
- Android基础教程之---Android状态栏提醒(Notification,NotificationManager)的使用!
- Android Material Design Library系列教程(一)
- android:Notification实现状态栏的通知
- Android 状态栏通知Notification、NotificationManager详解
- Android使用Notification在状态栏上显示通知
- Android系列教程之GridView组件的使用
- Android系列教程之TextView小组件的使用--附带超链接和跑马灯效果
- AndroidPN系列之一--Android Push Notification实现信息推送使用
- Android -- 状态栏通知Notification、NotificationManager详解
- 基于Android客户端的人人网开放平台开发系列教程
- android nexus 系列 源码刷机教程(ubuntu)
- Lance老师UI系列教程第二课->腾讯微博(下拉刷新+点击更多)listview的实现 (android)