Android开发文档——Notifications
2014-04-22 14:00
531 查看
原文地址: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
直接上代码吧:
大视图的话,只需添加如下代码:
管理Notification
更新Notification
移除Notification
1.用户自己点击通知栏里的“清除所有”按钮(就是右上角的叉叉,当然该Notification能被移除才行)。
2.mBuilder.setAutoCancel(),当用户点击该Notification时,会自动移除。
3.通过调用cancel()方法来移除之前设置的特定ID的Notification。
4.调用cancelAll()方法来移除所有的Notification。
在Notification里展示进度条
分两种情况:
进度的时间是确定的:
时间不确定:
时间有限,提取比较粗糙,请多见谅。
这里只介绍一下我认为重要的内容:
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 app Intent 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 stack stackBuilder.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 view inboxStyle.setBigContentTitle("Event tracker details:"); ... // Moves events into the big view for (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 updated int 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 thread new 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" state mBuilder.setProgress(100, incr, false); // Issues the notification mNotifyManager.notify(0, mBuilder.build());
//在这里处理你要做的事
// Sets an activity indicator for an operation of indeterminate length mBuilder.setProgress(0, 0, true); // Issues the notification mNotifyManager.notify(0, mBuilder.build());
时间有限,提取比较粗糙,请多见谅。
相关文章推荐
- Android开发文档之四种点击事件
- Android官方开发文档Training系列课程中文版:OpenGL绘图之图形绘制
- android开发官方文档(不定时更新)
- Android帮助文档翻译——开发指南(十五)获取用户位置
- Android 开发中的接口文档
- Android官方开发文档Training系列课程中文版:高效显示位图之在UI中展示位图
- Android开发基础1-概述和SDK安装、文档
- Android官方开发文档Training系列课程中文版:添加ActionBar之ActionBar浮层效果
- android 4.0 BLE开发官方文档介绍
- android 高效显示Bitmap - 开发文档翻译
- Android官方开发文档Training系列课程中文版:创建自定义View之View的优化
- Android官方开发文档Training系列课程中文版:电池续航时间优化之监测电池电量及充电状态
- android 4.0 BLE开发官方文档介绍
- Android开发文档标准翻译(02)一Application Fundamentals
- Android官方开发文档Training系列课程中文版:性能优化建议
- Android官方开发文档Training系列课程中文版:打印内容之图像打印
- Android帮助文档(第二部分)开发工具
- Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)
- Android开发文档翻译之-Services