状态栏通知Notification的简单使用

今天在学习Notification时同时参考了一些相关的博客,现在结合自身学习实际来总结一下。

在使用手机时,当有未接来电或者短消息时,通常会在手机屏幕上的状态栏上显示。而在Android中有提醒功能的也可以用AlertDialog,但是当使用AlertDialog 的时候,用户正在进行的操作将会被打断。因为当前焦点被AlertDialog得到。我们可以想像一下,当用户打游戏正爽的时候,这时候来了一条短信。如果这时候短信用AlertDialog提醒,用户必须先去处理这条提醒,从而才能继续游戏。用户可能会活活被气死。而使用Notification就不会带来这些麻烦事,用户完全可以打完游戏再去看这条短信。所以在开发中应根据实际需求,选择合适的控件。

 

状态栏通知涉及到两个类,一是Notification,它代表一个通知;另一个是NotificationManager,它是用于发送Notification的系统服务。

使用状态栏通知一般有4个步骤:

1、  通过getSystemService()方法获取NotificationManager服务。

2、  创建一个Notification对象,并为其设置各种属性。

3、  为Notification对象设置事件信息。

4、  通过NotificationManager类的notify()方法将通知发送到状态栏。

下面我们通过一个例子来实际操作一下:

首先是布局文件new_main.xml的使用:

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="fill_parent"  
 4     android:layout_height="fill_parent"  
 5     android:orientation="vertical" >  
 6   
 7     <Button  
 8         android:id="@+id/button1"  
 9         android:text="发送消息"  
10         android:layout_width="wrap_content"  
11         android:layout_height="wrap_content" />  
12   
13     <Button  
14         android:id="@+id/button2"  
15         android:text="清空消息"  
16         android:layout_width="wrap_content"  
17         android:layout_height="wrap_content" />  
18   
19 </LinearLayout>  
View Code

再创建一个Activity跳转后,新的Activity所需的页面content.xml:

 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
 3     android:layout_width="match_parent"  
 4     android:layout_height="match_parent"  
 5     android:orientation="vertical" >  
 6   
 7     <TextView  
 8         android:id="@+id/textView1"  
 9         android:layout_width="wrap_content"  
10         android:layout_height="wrap_content"  
11         android:text="test"  
12         android:textAppearance="?android:attr/textAppearanceMedium" />  
13   
14 </LinearLayout> 
View Code

接着创建一个ContentActivity(点击通知后跳转到的页面):

1 public class ContentActivity extends Activity{
2     @Override  
3     protected void onCreate(Bundle savedInstanceState) {  
4         // TODO Auto-generated method stub  
5         super.onCreate(savedInstanceState);  
6         setContentView(R.layout.content);  
7     }  
8 }
View Code

在MainActivity中定义两个变量:

final int NOTIFYID_1 = 123; // 第一个通知的ID
final int NOTIFYID_2 = 124; // 第二个通知的ID

然后重写onCreate()方法:

 1 @Override
 2     protected void onCreate(Bundle savedInstanceState) {
 3         super.onCreate(savedInstanceState);
 4         setContentView(R.layout.new_main);
 5         //获取通知管理器,用于发送通知
 6         final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 7         Button button1 = (Button) findViewById(R.id.button1);
 8         button1.setOnClickListener(new OnClickListener() {
 9             @Override
10             public void onClick(View v) {
11 
12                 Notification notify = new Notification();
13                 notify.icon = R.drawable.ic_launcher;
14                 notify.tickerText = "您有一个新的消息!";
15                 notify.when = System.currentTimeMillis();//获取当前时间,返回的是毫秒
16                 notify.defaults = Notification.DEFAULT_ALL;//设置默认的通知方式(声音、震动、闪光灯)
17                 notify.setLatestEventInfo(MainActivity.this, "通知1",
18                         "今天下午不上课啦!", null);
19                 notificationManager.notify(NOTIFYID_1, notify);
20                  Notification notify1 = new
21                  Notification(R.drawable.ic_launcher, "又是一条短信",
22                  System.currentTimeMillis());
23                 Intent intent = new Intent(MainActivity.this,
24                         ContentActivity.class);
25                 PendingIntent pendingIntent = PendingIntent.getActivity(
26                         MainActivity.this, 0, intent, 0);
27 //                Notification notify1 = new Notification.Builder(MainActivity.this)
28 //                        .setContentTitle("第二个通知 ")
29 //                        .setContentText("查看详情")
30 //                        .setSmallIcon(R.drawable.ic_launcher)
31 //                        .setContentIntent(pendingIntent).build();
32                 notify1.flags |= Notification.FLAG_AUTO_CANCEL;
33                 
34                 notify1.setLatestEventInfo(MainActivity.this, "通知2", "点击查看详情",
35                         pendingIntent);
36                 notificationManager.notify(NOTIFYID_2, notify1);
37             }
38         });
39 
40         Button button2 = (Button) findViewById(R.id.button2);
41         button2.setOnClickListener(new OnClickListener() {
42 
43             @Override
44             public void onClick(View v) {
45                 // //清除ID号为常量NOTIFYID_1的通知
46                 notificationManager.cancelAll(); // 清除全部通知
47 
48             }
49         });
50         
51 
52     }
View Code

最后在清单文件中添加相关代码:

<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.VIBRATE" />

<activity   android:name="com.topcsa.demo_notification.ContentActivity"
            android:label="通知"
            android:theme="@android:style/Theme.Dialog" />

好了,一个简单的Demo就做好了。

值得注意的是,在这两个通知的代码中有已经被遗弃的方法,在eclipse中会被黑线划掉。其相关文档是:

 

从中我们知道上面的方法和构造器在Android API 11中已被遗弃,下面我们来看看它推荐的方法:

上面实例的MainActivity中有这种推荐方法实现的代码(需要取消注释),在使用新的推荐代码时,请注释以下代码:

 Notification notify1 = new
 Notification(R.drawable.ic_launcher, "又是一条短信",
 System.currentTimeMillis());


notify1.setLatestEventInfo(MainActivity.this, "通知2", "点击查看详情",
pendingIntent);

运行程序就OK了,这时运行的就是推荐的方法了。

 

本文结束后又看到了一篇觉得不错的文章:http://blog.csdn.net/loongggdroid/article/details/17616509

posted @ 2014-04-16 17:17  银色的流星  阅读(735)  评论(0编辑  收藏  举报