`
Rainbow702
  • 浏览: 1065151 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类

Android NOtification 使用

阅读更多

一、 Notification 简介

 

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

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

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

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

 

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

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

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

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

 

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



 

二、 Notification的使用


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

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

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

 

2. StatusBar Notification

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

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

StatusBar Notification 基本步骤如下:

1 )得到 NotificationManager :

String ns = Context.NOTIFICATION_SERVICE; 

NotificationManager mNotificationManager = (NotificationManager) getSystemService( ns ); 

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

Notification notification = new Notification(); 

notification.icon = R.drawable.notification_icon; 

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

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

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

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

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

  3 )填充 Notification 的各个属性:

Context context = getApplicationContext(); 

CharSequence contentTitle = "My notification"; 

CharSequence contentText = "Hello World!"; 

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

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

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

 

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

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

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) 添加 remote view

通过 RemoteViews 设置 notification 中 View 的属性

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 )发送通知:

private static final int ID_NOTIFICATION = 1; 

mNotificationManager.notify(ID_NOTIFICATION, notification); 

 

 

3.Dialog Notification

3.1 AlertDialog

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

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

AlertDialog.Builder builder=newAlertDialog.Builder(this); 

builder.setMessage("Areyousureyouwanttoexit?") ; 

AlertDialog alert=builder.create(); 

 

3.2 ProcessDialog

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

ProgressDialog progressDialog=newProgressDialog(getApplicationContext()); 

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 

progressDialog.setIcon(R.drawable.alert_dialog_icon); 

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

progressDialog.setCancelable(false); 

 

原文地址: http://blog.csdn.net/ericyelai/article/details/6213046

  • 大小: 112.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics