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

复制去Google翻译翻译结果

时间: 2024-08-25 05:12:48

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

Android编程入门--简单闪屏界面

Manifest <activity android:name=".ui.activity.WelcomeActivity" android:theme="@style/SplashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android

Android应用icon和闪屏splash的尺寸

icon (尺寸为px) 目录 尺寸 (width * height) drawable 72 x 72 drawable-hdpi 72 x 72 drawable-ldpi 36 x 36 drawable-mdpi 48 x 48 drawable-xhdpi 96 x 96 drawable-xxhdpi 144 x 144 drawable-xxxhdpi 192 x 192 我查看支付宝钱包的时候,发现drawable-xxxhdpi里面的尺寸是180 x 180,不是上面标的192

android Theme启动APP闪屏处理

//1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme">       <item name="android:windowBackground">@drawable/ipod_bg</item>       <item name="android:windowNoTitle">true</

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

截图 启动页的 activity_splash.xml  我用了一张图片自己添加吧 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"

[android]切换activity闪屏问题

游戏登陆切换activity时,总是出现闪屏现象,解决方案,暂停线程100毫秒调用startActivity,闪屏解决,代码: @Override public void startActivity(final Intent intent) { new Thread() { @SuppressWarnings("static-access") public void run() { try { Thread.currentThread().sleep(100); } catch (In

android mediaplayer VideoPlayerManager 加载视频闪屏问题排查解决

Android VideoPlayer 在滚动列表实现item视频播放(ListView控件和RecyclerView),在列表滚动时点击屏幕列表暂停,在item视频播放区域,视频播放时会出现闪屏问题. 排查解决,VideoPlayerManager->MediaPlayerWrapper.java->prepare() :                     { .prepareAsync().set(State.)(!= ) {                             

Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法(转)

转载: Android App 启动页(Splash)黑/白闪屏现象产生原因与解决办法   首先感谢博主分享,本文作为学习记录 惊鸿一瞥 微信的启动页,相信大家都不陌生. 不知道大家有没有发现一个现象,微信每次启动的时候,是直接进入这个启动页面. 我的意思是,很多应用,往往会先白屏停顿一下后再进入启动页面(Splash).为了印证这一点,我把手机上所有的App都点了一遍.选几个例子 如下图: 微信:  斗鱼:  斗鱼和微信是直接进入了,他们的Splash页面. 知乎:  B站:  知乎和B站要先

Android学习笔记----闪屏主题

<!--闪屏主题--> <style name="welcome" parent="AppTheme"> <!--窗口透明--> <item name="android:windowIsTranslucent">true</item> </style>

Android中设置自己软件的铃声+震动

有时候一些通讯软件需要这些个功能,比如说收到短信,通知等,要求手机发出铃声,或震动,或发光以提示用户知晓. 往往手机都是有默认设置的,比如说用户开启了铃声+震动:只铃声不震动:完全静音等等... 这个时候就需要有一个规则了,起码软件的设置不能跟系统的冲突 规则: 1. 软件应该有个自己的设置配置文件,用以保存,自己的软件的提醒规则 2. 遵从系统的设置,比如说:系统是完全静音的,人家想睡觉啦,你软件虽然是铃声震动全开,也得乖乖闭嘴. 3. 如果有需要提醒了,先获取系统的配置,然后做逻辑判断给予什