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);
Android NOtification 使用(震动 闪屏 铃声)的更多相关文章
- Android应用icon和闪屏splash的尺寸
icon (尺寸为px) 目录 尺寸 (width * height) drawable 72 x 72 drawable-hdpi 72 x 72 drawable-ldpi 36 x 36 dra ...
- 033 Android App启动的闪屏效果+新手向导(多个图片滑动效果)+ViewPager使用
1.目标效果 App启动时,出现闪屏效果(利用动画实现). App新手使用时,会出现新手向导效果. 2.XML页面布局 (1)闪屏页面 <?xml version="1.0" ...
- android studio3.1 添加闪屏页面(启动欢迎界面)(例子简单无BUG)
截图 启动页的 activity_splash.xml 我用了一张图片自己添加吧 <?xml version="1.0" encoding="utf-8" ...
- android开发之splash闪屏页判断是否第一次进入app代码
package com.david.david.zhankudemo.activity; import android.app.Activity; import android.content.Con ...
- android131 360 01 闪屏页和主页面
主界面: 软件升级流程: 清单文件: <?xml version="1.0" encoding="utf-8"?> <manifest xml ...
- Android Notification实现推送消息过程中接受到消息端有声音及震动及亮屏提示
在Android Notification状态栏通知一文中,简单实现了消息的推送效果,这里就接着上文说一下,当用户接受到消息时的提示效果 // 5-增加震动及声音及亮屏 notification.de ...
- android 闪屏还是会出现黑屏问题
public class SplashActivity extends Activity{ @Override protected void onCreate(Bundle savedInstance ...
- android 的闪屏效果
android的闪屏效果,就是我们刚开始启动应用的时候弹出的界面或者动画,过2秒之后自动的跳转到主界面. 其实,实现这个效果很简单,使用Handler对象的postDelayed方法就可以实现.在这个 ...
- Android 实现闪屏页和右上角的倒计时跳转
效果图: 闪屏页用到了handler和CountDownTimer类,还需配置一下Activity的主题,这里是:android:theme="@android:style/Theme.No ...
随机推荐
- Excel连接字符串(Oledb)
Excel的连接字符串参数详解 2011-07-27 15:43:50| 分类: 编程专栏 | 标签:excel 连接字符串 参数 含义 |字号大中小 订阅 程序中读取Excel ...
- Nagios监控memcached
下载地址: http://search.cpan.org/CPAN/authors/id/Z/ZI/ZIGOROU/Nagios-Plugins-Memcached-0.02.tar.gz http: ...
- ORACLE安装过程中检查步骤出现的错误和解决方法【转】
Checking operating system requirements ...Expected result: One of redhat-3,redhat-4,SuSE-9,asianux-1 ...
- 【转】深入理解Java内存模型(七)——总结
处理器内存模型 顺序一致性内存模型是一个理论参考模型,JMM和处理器内存模型在设计时通常会把顺序一致性内存模型作为参照.JMM和处理器内存模型在设计时会对顺序一致性模型做一些放松,因为如果完全按照顺序 ...
- Eclipse搭建struts2环境
搭建struts2环境 大的方面分为三步: 1. 加入jar包 2. 在web.xml中配置struts2 3. 添加struts2的配置文件struts.xml 下面是详细步骤: 1. 新建一个Dy ...
- kafka教程
一.理论介绍(一)相关资料1.官方资料,非常详细: http://kafka.apache.org/documentation.html#quickstart2.有一篇翻译版,基本一致,有些细节不 ...
- curl 转载
CURLOPT_RETURNTRANSFER 选项: curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 如果成功只将结果返回,不自动输出任何内容. 如果失败返回F ...
- 自定义控件学习之菜鸟笔记一(Hello World)
一:引言 正在学习自定义控件的开发内容,做为菜鸟勤做笔记自然是必要任务了.本篇为本人学习自定义控件的学习笔记,借此和大家分享一下学习过程吧. 本人学习喜欢先看到具体的实例,然后再深入学习,故先从一个简 ...
- JS滚动条下拉事件
<script type="text/javascript"> window.onscroll = function(){ var t = document.docum ...
- You don't have permission to access /phpmyadmin/main.php on this server.
wamp 安装后,打开首页.出现问题,信息如下: “You don't have permission to access /phpmyadmin/main.php on this server.” ...