一、 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);

 

Android NOtification 使用(震动 闪屏 铃声)的更多相关文章

  1. Android应用icon和闪屏splash的尺寸

    icon (尺寸为px) 目录 尺寸 (width * height) drawable 72 x 72 drawable-hdpi 72 x 72 drawable-ldpi 36 x 36 dra ...

  2. 033 Android App启动的闪屏效果+新手向导(多个图片滑动效果)+ViewPager使用

    1.目标效果 App启动时,出现闪屏效果(利用动画实现). App新手使用时,会出现新手向导效果. 2.XML页面布局 (1)闪屏页面 <?xml version="1.0" ...

  3. android studio3.1 添加闪屏页面(启动欢迎界面)(例子简单无BUG)

    截图 启动页的 activity_splash.xml  我用了一张图片自己添加吧 <?xml version="1.0" encoding="utf-8" ...

  4. android开发之splash闪屏页判断是否第一次进入app代码

    package com.david.david.zhankudemo.activity; import android.app.Activity; import android.content.Con ...

  5. android131 360 01 闪屏页和主页面

    主界面: 软件升级流程: 清单文件: <?xml version="1.0" encoding="utf-8"?> <manifest xml ...

  6. Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示

    在Android Notification状态栏通知一文中,简单实现了消息的推送效果,这里就接着上文说一下,当用户接受到消息时的提示效果 // 5-增加震动及声音及亮屏 notification.de ...

  7. android 闪屏还是会出现黑屏问题

    public class SplashActivity extends Activity{ @Override protected void onCreate(Bundle savedInstance ...

  8. android 的闪屏效果

    android的闪屏效果,就是我们刚开始启动应用的时候弹出的界面或者动画,过2秒之后自动的跳转到主界面. 其实,实现这个效果很简单,使用Handler对象的postDelayed方法就可以实现.在这个 ...

  9. Android 实现闪屏页和右上角的倒计时跳转

    效果图: 闪屏页用到了handler和CountDownTimer类,还需配置一下Activity的主题,这里是:android:theme="@android:style/Theme.No ...

随机推荐

  1. 【C#学习笔记】二、面向对象编程

    2.1 抽象类与接口 1)概念 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类 ...

  2. mobx react

    目录结构: Model/index.js 'use strict'; import { action, autorun, observable, computed } from "mobx& ...

  3. BeanUtils--内省加强

    BeanUtils就是一个处理Bean的工具包.内部也是使用内省.但对内省做了加强. Bean的set |get不用再成对出现 核心类: BeanUtils. 1.导包

  4. 编辑一个类库项目 即*.csproj这个文件的正确方式

    以前总是用记事本打开,删除一些或增加一些已修改的文件 今天才知道,正确的方式为: 右键单击类库,选择“卸载项目”,然后再右键单击已卸载变为灰色的类库,选择“编辑*.csproj” 编辑完了重新加载一下 ...

  5. code:blocks 编译环境设置

    1.  支持C99 在菜单settings->compiler settings->comiler settings->Other options 添加: -std=c99 2. 支 ...

  6. 【回忆1314】抽奖之Flash大转盘

    1.搭建JS与Flash互通的环境 function thisMovie(movieName){ if (window.document[movieName]) { return window.doc ...

  7. jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Se ...

  8. c++ 高效文本读写

    上数据结构课,做project,数据老师要求我们多做测试,而文本文件可以有效记录这些东东,这样我想起了文件的读写,下面是渣渣我个人的一些想法,大神们看见有错的,尽管指出(orz~~~囧,木有人看我的呀 ...

  9. java多线程下单例的实现

    Abstract 在开发中,如果某个实例的创建需要消耗很多系统资源,那么我们通常会使用惰性加载机制,也就是说只有当使用到这个实例的时候才会创建这个实例,这个好处在单例模式中得到了广泛应用.这个机制在s ...

  10. 【转】VirtualBox direct access to SD Card in Windows--不错

    原文网址:http://www.sandyscott.net/2013/08/14/virtualbox-direct-drive-access/ I’ve trying to get my Rasp ...