Notification 使用

来源:互联网 发布:台湾中医药资料数据库 编辑:程序博客网 时间:2024/06/05 15:48

一、Notification简介

android系统中,在应用程序可能会遇到几种情况需要通知用户,有的需要用户回应,有的则不需要,例如:

*当保存文件等事件完成,应该会出现一个小的消息,以确认保存成功。

*如果应用程序在后台运行,需要用户的注意,应用程序应该创建一个通知,允许用户在他或她的回应提供便利

*如果应用程序正在执行的工作,用户必须等待(如装载文件),应用程序应该显示进度或等待提醒。

针对这些情况,android都提供了不同的提醒方式。主要包括下面几种:

1.ToastNotification是指出现在屏幕上的暂时性通知,这种通知用于传达一些告知类型的消息,短暂停留后会自动消失,无需用户交互。比如告知下载已完成等。(ToastNoification这个说法最早是源于一个前MSN员工的提法,因为MSN的消息提醒是从底部向上轻弹,形式上很像一个面包从烤面包机中弹起的样子,所以称之为ToastNoification)

2.StatusBarNotification是指以一个图标或者滚动条文本的形式出现在系统顶部状态栏上的通知。当应用程序处于后台运行状态时,这种方式比较合适。这种通知形式的好处是既能即使被关注到,又无需打断当前任务,可以从顶部下拉查看通知摘并做选择性处理。

3.DialogNotification类似于iOSAlertNotification,以对话窗口的形式出现在屏幕上,用于重要或需及时处理的通知。

下面我们先了解以下Androidnotification的整个架构。前二种提醒方式都是由NotificationManagerService,而DialogNotification,则是弹出一个窗口形式实现的,因为这种提醒方式大多是针对当前应用程序或进程,所以它只是一种简单且直观的表达方式。


二、Notification的使用

1.Toast

ToastAndroid中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失

 

//使用TOAST方法显示结果内容
ToasttextToast=Toast.makeText(this, "
提示内容
",Toast.LENGTH_LONG);

//...这里也可以对Toast添加一些属性
textToast.show();

2.StatusBar Notification

StatusBarNotification是在系统状态栏上增加了一个状态栏图标,并在“通知“窗口中显示提示信息。当用户选择展开邮件,Android就会发送一个通知(通常是推出一个活动)定义的意向。您也可以配置通知,提醒和声音,震动的用户,并在设备上闪烁的灯光。

这样的通知是很理想的工作时,您的应用程序在后台服务,需要通知有关事件的用户。如果您需要提醒有关事件已经发生,而你的活动仍可以在当前焦点,此时可以考虑使用一个对话框通知代替。

 

StatusBarNotification基本步骤如下:

 

1)得到NotificationManager

Stringns= Context.NOTIFICATION_SERVICE;

NotificationManagermNotificationManager = (NotificationManager) getSystemService(ns);

2)创建一个新的Notification对象:

Notificationnotification = new Notification();

notification.icon= R.drawable.notification_icon;

//也可以使用稍微复杂一些的方式创建Notification

inticon = R.drawable.notification_icon; 通知图标

CharSequencetickerText = "Hello"; //状态栏(StatusBar)显示的通知文本提示

longwhen = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

Notificationnotification = new Notification(icon, tickerText, when);

 

3)填充Notification的各个属性:

Contextcontext = getApplicationContext();

CharSequencecontentTitle = "My notification";

CharSequencecontentText = "Hello World!";

IntentnotificationIntent = new Intent(this, MyClass.class);

PendingIntentcontentIntent = PendingIntent.getActivity(this, 0,notificationIntent,0);

notification.setLatestEventInfo(context,contentTitle, contentText, contentIntent);

 

Notification提供了丰富的手机提示方式:

a)在状态栏(StatusBar)显示的通知文本提示,如:

notification.tickerText= "hello";

b)发出提示音,如:

notification.defaults|= Notification.DEFAULT_SOUND;

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

notification.sound= Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

 

c)手机振动,如:

notification.defaults|= Notification.DEFAULT_VIBRATE;

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

notification.vibrate= vibrate;

 

d)LED灯闪烁,如:

notification.defaults|= Notification.DEFAULT_LIGHTS;

notification.ledARGB= 0xff00ff00;

notification.ledOnMS= 300;

notification.ledOffMS= 1000;

notification.flags|= Notification.FLAG_SHOW_LIGHTS;

 

e)添加remoteview

通过RemoteViews设置notificationView的属性

notification.contentView= new RemoteViews(getApplication().getPackageName(),R.layout.custom_dialog);

notification.contentView.setProgressBar(R.id.pb,100, 0, false);

notification.contentView.setTextViewText(R.id.tv,"进度"+ _progress+ "%");

4)发送通知:

privatestatic finalintID_NOTIFICATION = 1;

mNotificationManager.notify(ID_NOTIFICATION,notification);


3.Dialog Notification

3.1AlertDialog

为了创建一个警告对话框,使用AlertDialog.Builder子类。通过AlertDialog.Builder

(Context)获取一个构造器然后使用这个类的公共方法来定义警告对话框的所有属性。当得到构造器后,通过create().方法来获取警告对话框对象。有时我是不调用create()的,而是在设置好了后直接调用show()显示AlertDialog

AlertDialog.Builderbuilder=newAlertDialog.Builder(this);

builder.setMessage("Areyousureyouwanttoexit?")

AlertDialogalert=builder.create();

 

3.2ProcessDialog

ProgressDialogAlertDialog类的一个扩展,可以为一个未定义进度的任务显示一个旋转轮形状的进度动画,或者为一个指定进度的任务显示一个进度条。

ProgressDialogprogressDialog=newProgressDialog(getApplicationContext());

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

progressDialog.setIcon(R.drawable.alert_dialog_icon);

progressDialog.setMessage("Loading...");

progressDialog.setCancelable(false);


(1)、使用系统定义的Notification

以下是使用示例代码:

//创建一个NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//定义Notification的各种属性

int icon = R.drawable.icon; //通知图标

CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

//用上面的属性初始化Nofification

Notification notification = new Notification(icon,tickerText,when);

/*

* 添加声音

* notification.defaults |=Notification.DEFAULT_SOUND;

* 或者使用以下几种方式

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

* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

* 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"

* 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音

*/

/*

* 添加振动

* notification.defaults |= Notification.DEFAULT_VIBRATE;

* 或者可以定义自己的振动模式:

* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒

* notification.vibrate = vibrate;

* long数组可以定义成想要的任何长度

* 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动

*/

/*

* 添加LED灯提醒

* notification.defaults |= Notification.DEFAULT_LIGHTS;

* 或者可以自己的LED提醒模式:

* notification.ledARGB = 0xff00ff00;

* notification.ledOnMS = 300; //亮的时间

* notification.ledOffMS = 1000; //灭的时间

* notification.flags |= Notification.FLAG_SHOW_LIGHTS;

*/

/*

* 更多的特征属性

* notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知

* notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知

* notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中

* notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,

* //经常与FLAG_ONGOING_EVENT一起使用

* notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部

* //如果要使用此字段,必须从1开始

* notification.iconLevel = ; //

*/

//设置通知的事件消息

Context context = getApplicationContext(); //上下文

CharSequence contentTitle = "My Notification"; //通知栏标题

CharSequence contentText = "Hello World!"; //通知栏内容

Intent notificationIntent = new Intent(this,Main.class); //点击该通知后要跳转的Activity

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

//把Notification传递给NotificationManager

mNotificationManager.notify(0,notification);

如果想要更新一个通知,只需要在设置好notification之后,再次调用 setLatestEventInfo(),然后重新发送一次通知即可,即再次调用notify()。


(2)、使用自定义的Notification

要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段。以下示例代码是完整步骤:

//1、创建一个自定义的消息布局 view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<ImageView android:id="@+id/image" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:layout_marginRight="10dp" />

<TextView android:id="@+id/text" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:textColor="#000" />

</LinearLayout>

//2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);

notification.contentView = contentView;

//3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//4、发送通知

mNotificationManager.notify(2,notification);

//以下是全部示例代码

//创建一个NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//定义Notification的各种属性

int icon = R.drawable.icon; //通知图标

CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

//用上面的属性初始化Nofification

Notification notification = new Notification(icon,tickerText,when);

RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);

contentView.setImageViewResource(R.id.image, R.drawable.iconempty);

contentView.setTextViewText(R.id.text, "Hello,this is JC");

notification.contentView = contentView;

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//把Notification传递给NotificationManager

mNotificationManager.notify(0,notification);




原创粉丝点击