Notification使用详解之一:基础应用

 在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。

首先说明一下我们需要实现的功能是:在程序启动时,发出一个通知,这个通知在软件运行过程中一直存在,相当于qq的托盘一样;然后再演示一下普通的通知和自定义视图通知。

那我们就先建立一个名为notification的项目,然后编辑/res/layout/main.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>  
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    android:orientation="vertical"  
04.    android:layout_width="fill_parent"  
05.    android:layout_height="fill_parent">  
06.    <TextView  
07.        android:layout_width="fill_parent"  
08.        android:layout_height="wrap_content"  
09.        android:gravity="center"  
10.        android:text="点击按钮进入演示界面"/>  
11.    <Button  
12.        android:layout_width="fill_parent"  
13.        android:layout_height="wrap_content"  
14.        android:text="notify activity"  
15.        android:onClick="notify"/>  
16.</LinearLayout>  

然后编辑MainActivity.java文件,代码如下:

package com.scott.notification;  
02.  
03.import android.app.Activity;  
04.import android.app.Notification;  
05.import android.app.NotificationManager;  
06.import android.app.PendingIntent;  
07.import android.content.Context;  
08.import android.content.Intent;  
09.import android.os.Bundle;  
10.import android.view.View;  
11.  
12.public class MainActivity extends Activity {  
13.  
14.    private static final int ONGOING_ID = 0;  
15.    private NotificationManager mNotificationManager;  
16.      
17.    @Override  
18.    public void onCreate(Bundle savedInstanceState) {  
19.        super.onCreate(savedInstanceState);  
20.        setContentView(R.layout.main);  
21.        setUpNotification();  
22.    }  
23.  
24.    private void setUpNotification() {  
25.        Context context = this;  
26.  
27.        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
28.  
29.        int icon = R.drawable.ongoing;  
30.        CharSequence tickerText = "程序已启动";  
31.        long when = System.currentTimeMillis();  
32.          
33.        //新建一个Notification实例  
34.        Notification notification = new Notification(icon, tickerText, when);  
35.  
36.        // 把通知放置在"正在运行"栏目中  
37.        notification.flags |= Notification.FLAG_ONGOING_EVENT;  
38.  
39.        CharSequence contentTitle = "Notification示例";  
40.        CharSequence contentText = "程序正在运行中,点击此处跳到演示界面";  
41.          
42.        Intent intent = new Intent(context, NotifyActivity.class);  
43.        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);  
44.  
45.        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
46.  
47.        mNotificationManager.notify(ONGOING_ID, notification);  
48.    }  
49.  
50.    public void notify(View view) {  
51.        Intent intent = new Intent(this, NotifyActivity.class);  
52.        startActivity(intent);  
53.    }  
54.      
55.    @Override  
56.    public void onBackPressed() {  
57.        super.onBackPressed();  
58.        finish();  
59.        //取消一个通知  
60.        mNotificationManager.cancel(ONGOING_ID);  
61.        //结束进程  
62.        android.os.Process.killProcess(android.os.Process.myPid());  
63.        System.exit(0);  
64.    }  
65.} 

大家看以看到,在程序主界面启动时会发出一个通知,并且将这个通知放置在“正在运行”栏目中,这样在软件运行过程中它就会始终存在;另外,在上面的代码中,在用户按下回退按钮时,我们使用NotificationManager.cancel(int id)方法取消这个通知。

先来看一下运行效果如何:(注意,这篇文章是2011年写的, Notification(int icon, CharSequence tickerText, long when)和setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 已经不再使用了,用Notification.Builder代替。

 

 

点击主界面的按钮和Ongoing栏目中的通知均能跳转到NotifyActivity界面,在这个界面中,我们主要演示一下普通通知和自定义视图通知的使用。

我们先要在/res/layout/目录下添加一个notify.xml布局文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>  
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    android:orientation="vertical"  
04.    android:layout_width="fill_parent"  
05.    android:layout_height="fill_parent">  
06.    <Button  
07.        android:layout_width="fill_parent"  
08.        android:layout_height="wrap_content"  
09.        android:text="normal notify"  
10.        android:onClick="normalNotify"/>  
11.    <Button  
12.        android:layout_width="fill_parent"  
13.        android:layout_height="wrap_content"  
14.        android:text="custom notify"  
15.        android:onClick="customNotify"/>  
16.</LinearLayout> 

因为要演示自定义通知视图,我们需要定义一个自定义通知视图的布局文件,摆放我们自己的布局组件,因此在/res/layout/目录下添加一个custom_notification_layout.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>  
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.  android:orientation="horizontal"  
04.  android:layout_width="fill_parent"  
05.  android:layout_height="fill_parent"  
06.  android:padding="3dp">  
07.  <ImageView  
08.    android:id="@+id/imageView"  
09.    android:layout_width="wrap_content"  
10.    android:layout_height="fill_parent"  
11.    android:layout_marginRight="10dp"/>  
12.  <TextView   
13.    android:id="@+id/textView"           
14.    android:layout_width="wrap_content"                
15.    android:layout_height="fill_parent"                
16.    android:textColor="#000"/>  
17.</LinearLayout>  

然后就来看一下NotifyActivity.java的代码:

package com.scott.notification;  
02.  
03.import android.app.Activity;  
04.import android.app.Notification;  
05.import android.app.NotificationManager;  
06.import android.app.PendingIntent;  
07.import android.content.Context;  
08.import android.content.Intent;  
09.import android.os.Bundle;  
10.import android.view.View;  
11.import android.widget.RemoteViews;  
12.  
13.public class NotifyActivity extends Activity {  
14.      
15.    //注意,如果不想覆盖前一个通知,需设置不同的ID  
16.    private static final int NORMAL_NOTIFY_ID = 1;  
17.    private static final int CUSTOM_NOTIFY_ID = 2;  
18.      
19.    @Override  
20.    protected void onCreate(Bundle savedInstanceState) {  
21.        super.onCreate(savedInstanceState);  
22.        setContentView(R.layout.notify);  
23.    }  
24.      
25.    // 普通通知事件  
26.    public void normalNotify(View view) {  
27.        Context context = this;  
28.  
29.        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
30.  
31.        int icon = android.R.drawable.stat_notify_voicemail;  
32.        CharSequence tickerText = "普通通知";  
33.        long when = System.currentTimeMillis();  
34.        Notification notification = new Notification(icon, tickerText, when);  
35.  
36.        // 设定声音  
37.        notification.defaults |= Notification.DEFAULT_SOUND;  
38.          
39.        //设定震动(需加VIBRATE权限)  
40.        notification.defaults |= Notification.DEFAULT_VIBRATE;  
41.          
42.        // 设定LED灯提醒  
43.        notification.defaults |= Notification.DEFAULT_LIGHTS;  
44.          
45.        // 设置点击此通知后自动清除  
46.        notification.flags |= Notification.FLAG_AUTO_CANCEL;  
47.  
48.        CharSequence contentTitle = "普通通知的标题";  
49.        CharSequence contentText = "通知的内容部分,一段长长的文字...";  
50.        Intent intent = new Intent(context, TargetActivity.class);  
51.        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);  
52.  
53.        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);  
54.  
55.        mNotificationManager.notify(NORMAL_NOTIFY_ID, notification);  
56.    }  
57.  
58.    // 个性化通知点击事件  
59.    public void customNotify(View view) {  
60.        Context context = this;  
61.  
62.        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
63.  
64.        int icon = android.R.drawable.stat_notify_voicemail;  
65.        CharSequence tickerText = "自定义通知";  
66.        long when = System.currentTimeMillis();  
67.        Notification notification = new Notification(icon, tickerText, when);  
68.  
69.        notification.flags |= Notification.FLAG_AUTO_CANCEL;  
70.  
71.        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);  
72.        contentView.setImageViewResource(R.id.imageView, R.drawable.smile);  
73.        contentView.setTextViewText(R.id.textView, "这是一个个性化的通知视图,代替了系统默认的通知视图.");  
74.        // 指定个性化视图  
75.        notification.contentView = contentView;  
76.  
77.        Intent intent = new Intent(context, TargetActivity.class);  
78.        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);  
79.        // 指定内容意图  
80.        notification.contentIntent = contentIntent;  
81.  
82.        mNotificationManager.notify(CUSTOM_NOTIFY_ID, notification);  
83.    }  
84.}  

注意,上边在添加一个普通通知时使用到了震动,所以需要在AndroidManifest.xml中加入相关权限:

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

除了使用代码中的默认通知属性之外,用户也可以自定义属性值:

1.自定义声音:

notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");  

2.自定义震动方式:

long[] vibrate = {0, 100, 200, 300};  
02.notification.vibrate = vibrate;  

这个数组定义了交替的震动和关闭,以毫秒为单位。第一个值是等待多久开始震动,第二个值是第一次震动的时间,第三个值是停止震动的时间,以此类推。定义多长时间都行,但是不能设置为重复。

3.自定义闪光方式:

notification.ledARGB = 0xff00ff00;  
02.notification.ledOnMS = 300;  
03.notification.ledOffMS = 1000;  
04.notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

上边几行代码表示绿灯先显示300毫秒然后关闭一秒钟。如果设备不支持指定的颜色,则会按照最接近的颜色显示。

如果全部都使用默认值时,可以用以下代码代替程序中的几行设定defaults的代码:

notification.defaults |= Notification.DEFAULT_ALL;  

注意,在自定义以上属性时,如果defaults中与之相关的默认值已设置,则自定义属性就会失效。

然后再来介绍一下几种常用的flags:

1.FLAG_AUTO_CANCEL:在用户查看通知信息后自动关闭通知;

2.FLAG_INSISTENT:在用户响应之前一直重复;

3.FLAG_ONGOING_EVENT:放置在“正在运行”栏目中,表示程序正在运行,可见状态,或是后台运行;

4.FLAG_NO_CLEAR:查看通知后不会自动取消,对一直进行中的通知非常有用。

在上面的程序中,点击通知后跳转到TargetActivity界面,这个界面非常简单,就显示一串文字,这里就不必多说了。

最后让我们演示一下效果:

关于notification的相关知识,今天先介绍到这里,以后会继续介绍更深入的使用技巧。

转自:http://blog.csdn.net/liuhe688/article/details/6586353

posted @ 2014-09-03 18:12  zuiaishenlin  阅读(158)  评论(0)    收藏  举报