Android开发文档——Notifications
来源:互联网 发布:淘宝金冠 编辑:程序博客网 时间:2024/05/29 06:37
原文地址:http://developer.android.com/guide/topics/ui/notifiers/notifications.html
这里只介绍一下我认为重要的内容:
- Notification展示元素
普通视图
- 内容标题(Content title)
- 大icon(Large icon)
- 内容文本(Content text)
- 内容信息(Content info)
- 小icon(Small icon)
- 创建时间(Time that the notification was issued. )可以使用setWhen()方法来修改。
大视图
比普通视图多了7:详细内容。
- 创建一个Notification
直接上代码吧:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");// Creates an explicit intent for an Activity in your appIntent resultIntent = new Intent(this, ResultActivity.class);// The stack builder object will contain an artificial back stack for the// started Activity.// This ensures that navigating backward from the Activity leads out of// your application to the Home screen.TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);// Adds the back stack for the Intent (but not the Intent itself)stackBuilder.addParentStack(ResultActivity.class);// Adds the Intent that starts the Activity to the top of the stackstackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );mBuilder.setContentIntent(resultPendingIntent);NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// mId allows you to update the notification later on.mNotificationManager.notify(mId, mBuilder.build());大视图的话,只需添加如下代码:
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();String[] events = new String[6];// Sets a title for the Inbox style big viewinboxStyle.setBigContentTitle("Event tracker details:");...// Moves events into the big viewfor (int i=0; i < events.length; i++) { inboxStyle.addLine(events[i]);}// Moves the big view style object into the notification object.mBuilder.setStyle(inBoxStyle);
- 管理Notification
更新Notification
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// Sets an ID for the notification, so it can be updatedint notifyID = 1;mNotifyBuilder = new NotificationCompat.Builder(this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.drawable.ic_notify_status)numMessages = 0;// Start of a loop that processes data and then notifies the user... mNotifyBuilder.setContentText(currentText) .setNumber(++numMessages); // Because the ID remains unchanged, the existing notification is // updated. mNotificationManager.notify( notifyID, mNotifyBuilder.build());...
移除Notification
1.用户自己点击通知栏里的“清除所有”按钮(就是右上角的叉叉,当然该Notification能被移除才行)。
2.mBuilder.setAutoCancel(),当用户点击该Notification时,会自动移除。
3.通过调用cancel()方法来移除之前设置的特定ID的Notification。
4.调用cancelAll()方法来移除所有的Notification。
- 在Notification里展示进度条
分两种情况:
进度的时间是确定的:
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);mBuilder = new NotificationCompat.Builder(this);mBuilder.setContentTitle("Picture Download") .setContentText("Download in progress") .setSmallIcon(R.drawable.ic_notification);// Start a lengthy operation in a background threadnew Thread( new Runnable() { @Override public void run() { int incr; // Do the "lengthy" operation 20 times for (incr = 0; incr <= 100; incr+=5) { // Sets the progress indicator to a max value, the // current completion percentage, and "determinate" // state mBuilder.setProgress(100, incr, false); // Displays the progress bar for the first time. mNotifyManager.notify(0, mBuilder.build()); // Sleeps the thread, simulating an operation // that takes time try { // Sleep for 5 seconds Thread.sleep(5*1000); } catch (InterruptedException e) { Log.d(TAG, "sleep failure"); } } // When the loop is finished, updates the notification mBuilder.setContentText("Download complete") // Removes the progress bar .setProgress(0,0,false); mNotifyManager.notify(ID, mBuilder.build()); } }// Starts the thread by calling the run() method in its Runnable).start();时间不确定:
// Sets the progress indicator to a max value, the current completion// percentage, and "determinate" statemBuilder.setProgress(100, incr, false);// Issues the notificationmNotifyManager.notify(0, mBuilder.build());
//在这里处理你要做的事
// Sets an activity indicator for an operation of indeterminate lengthmBuilder.setProgress(0, 0, true);// Issues the notificationmNotifyManager.notify(0, mBuilder.build());
时间有限,提取比较粗糙,请多见谅。
0 0
- Android开发文档——Notifications
- 《Android开发从零开始》——19.Status Bar Notifications学习
- 《Android开发从零开始》——40.Status Bar Notifications
- Android开发指南(38) —— Status Bar Notifications(翻译)
- Android开发指南(38) —— Status Bar Notifications
- Android——Notifications笔记
- Android——Notifications笔记
- [翻译]Android官方文档 - 通知(Notifications)
- 【Android】Parse 开发笔记(4)—— Push Notifications(上)
- 【Android 开发】:通知之Notifications Toast
- 【Android 开发】:通知之 Status Bar Notifications
- Android Notifications
- Android: notifications
- Android Notifications
- Android Notifications
- android - Notifications
- Android Notifications
- 《iOS Human Interface Guidelines》——Notifications
- 第九周项目五
- Eclipse: convert “dynamic web module” to “web fragment module”
- 关于typedef的用法总结
- C++ 访问http接口
- C/C++日常学习总结(第一篇)const用法及printf的执行顺序
- Android开发文档——Notifications
- thinkphp多表查询及分页实现
- 【linux设备模型】之platform设备驱动
- Spring MVC拦截器,登录拦截简单配置
- Android Annotations 介绍
- Linux服务器集群系统——LVS集群的体系结构
- UVa 10970 - Big Chocolate
- hdfs安全模式导致hbase执行失败,提示zookeeper相关错误
- 导出CCS3.3数据及使用matlab处理的方法