【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
2015-03-06 16:52
651 查看
应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信)。类NotificationManager 用来处理Notification, NotificationManager可以:
在Status Bar上显示一个新的图标。
在Extended status bar 窗口上显示附加信息或是启动一个Activity。
显示背光/LED。
使设备震动。
发出声音等。
对于一些没有UI的应用程序组件(如Broadcast Receiver, Services)或是非活动状态的Activity,Notification是推荐使用的可以提醒用户注意的方法。
Notification通常是在Status Bar上显示图标或是文字,此时用户如果想了解Notification的详细内容,可以按住Status Bar下拉显示Expanded Status bar 窗口,在Expanded Status bar窗口显示该Notification详情并可以启动对应的Activity。
IncomingMessage 示例介绍了Notification的一般用法:
1. 首先是取得NotificationManager 对象:
2. 然后创建Notification,创建Notification时指定显示在Status bar的图标,文字以及显示Notification的时间:
3. 然后定义当用户打开Extented status windows窗口时的标题及详情。Notification常常代表了一个请求或者需要引起注意的事件,因此可以指定一个PendingIntent来响应用户点击这个Notification。
4. 最后是触发这个Notification:
一般来说对应同一个事件可以使用同一个Notification来通知用户,nm.notify的第一个参数为Notification 的ID,类型为整数。 可以使用同一个ID来表示同一个Notification,也可以使用这个ID来取消这个Notification,在IncomingMessage 中当用户点击显示了这个IncomingMessage详情后,会取消这个Notification(类IncomingMessageView中)。
在Status Bar上显示一个新的图标。
在Extended status bar 窗口上显示附加信息或是启动一个Activity。
显示背光/LED。
使设备震动。
发出声音等。
对于一些没有UI的应用程序组件(如Broadcast Receiver, Services)或是非活动状态的Activity,Notification是推荐使用的可以提醒用户注意的方法。
Notification通常是在Status Bar上显示图标或是文字,此时用户如果想了解Notification的详细内容,可以按住Status Bar下拉显示Expanded Status bar 窗口,在Expanded Status bar窗口显示该Notification详情并可以启动对应的Activity。
IncomingMessage 示例介绍了Notification的一般用法:
1. 首先是取得NotificationManager 对象:
// look up the notification manager service NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2. 然后创建Notification,创建Notification时指定显示在Status bar的图标,文字以及显示Notification的时间:
// construct the Notification object. Notification notif = new Notification(R.drawable.stat_sample, tickerText, System.currentTimeMillis());
3. 然后定义当用户打开Extented status windows窗口时的标题及详情。Notification常常代表了一个请求或者需要引起注意的事件,因此可以指定一个PendingIntent来响应用户点击这个Notification。
// The details of our fake message CharSequence from = "Joe"; CharSequence message; switch ((new Random().nextInt()) % 3) { case 0: message = "r u hungry? i am starved"; break; case 1: message = "im nearby u"; break; default: message = "kthx. meet u for dinner. cul8r"; break; } // The PendingIntent to launch our activity if the user selects this // notification. Note the use of FLAG_CANCEL_CURRENT so that, if there // is already an active matching pending intent, cancel it and replace // it with the new array of Intents. PendingIntent contentIntent = PendingIntent.getActivities(this, 0, makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT); // The ticker text, this uses a formatted string so our message could be localized String tickerText = getString(R.string.imcoming_message_ticker_text, message); // construct the Notification object. Notification notif = new Notification(R.drawable.stat_sample, tickerText, System.currentTimeMillis()); // Set the info for the views that show in the notification panel. notif.setLatestEventInfo(this, from, message, contentIntent); // We'll have this notification do the default sound, vibration, and led. // Note that if you want any of these behaviors, you should always have // a preference for the user to turn them off. notif.defaults = Notification.DEFAULT_ALL;
4. 最后是触发这个Notification:
// Note that we use R.layout.incoming_message_panel as the ID for // the notification. It could be any integer you want, but we use // the convention of using a resource id for a string related to // the notification. It will always be a unique number within your // application. nm.notify(R.string.imcoming_message_ticker_text, notif);
一般来说对应同一个事件可以使用同一个Notification来通知用户,nm.notify的第一个参数为Notification 的ID,类型为整数。 可以使用同一个ID来表示同一个Notification,也可以使用这个ID来取消这个Notification,在IncomingMessage 中当用户点击显示了这个IncomingMessage详情后,会取消这个Notification(类IncomingMessageView中)。
nm.cancel(R.string.imcoming_message_ticker_text);
相关文章推荐
- 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
- 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
- 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动
- 【起航计划 015】2015 起航计划 Android APIDemo的魔鬼步伐 14 App->Activity->Translucent Blur 模糊背景
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
- 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder
- 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
- 【起航计划 007】2015 起航计划 Android APIDemo的魔鬼步伐 06 App->Activity->Forwarding Activity启动另外一个Activity finish()方法
- 【起航计划 016】2015 起航计划 Android APIDemo的魔鬼步伐 15 App->Activity->Wallpaper 系统壁纸作为当前Activity的背景
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
- 【起航计划 019】2015 起航计划 Android APIDemo的魔鬼步伐 18 App->Device Admin 设备管理器 DeviceAdminReceiver DevicePolicyManager PreferenceActivity的使用
- 【起航计划 023】2015 起航计划 Android APIDemo的魔鬼步伐 22 App->Menu->Inflate from XML 使用xml资源展示菜单
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 011】2015 起航计划 Android APIDemo的魔鬼步伐 10 App->Activity->Reorder Activities 后退栈 Intent FLAG