reference : http://castlerockresearch.in/blog/dev/2010/07/16/ongoing-notifications-in-android

Notifications in Android are a great way to notify the user about various events going on inside your app. The very basic notification type, generally used on Android is the “Normal Notification“. Your app creates a notification which is visible on the status bar. When the user pulls down the notification bar, he will see a description of what the notification is about. If interested to know more about what has happened, the user clicks on the notification and is, most of the times, taken to an Activity which shows the latest changes/events. Clicking on the notification, generally triggers an Intent which can do several things. You can start an Activity, start a background service or send a broadcast. This is the default behavior of a Notification on Android.? You can find an example of creating a basic Android notification here.

There can be situations where, your app just wants the user to know that you are doing something in the background. For example, when the Gmail app on your phone is syncing your contacts, calendar or mails, you can see a status bar notification (animated) which lets the user know that the GMail app is syncing. That’s all. But if you try to pull down the notification bar, you will not see any details about that.

This kind of a notification is the “Ongoing Notification”. A better example is the notification that you get on the status bar when you are in a call. You can pull down that notification to see the details, for example the duration of the ongoing call which is updated every second. This also uses a custom view for the notification which is different from the default notification. One important difference is that, you cannot “clear” the “Ongoing notifications”. We can create similar kind of notifications for our apps as well.

In this example, I will be creating a similar “Ongoing Notification” along with an animated icon. Let’s start coding.

1. For animating the notification icon, you need to use something called a LevelListDrawable. For this example I have created 4 different images which would be used in the LevelListDrawable and will give an impression of an animation.

Listing of level_list.xml? (Place this file in your res/drawable folder)

   1: <level-list xmlns:android="http://schemas.android.com/apk/res/android">  ?2:     <item android:maxLevel="0" android:drawable="@drawable/process_1" />  ?3:     <item android:maxLevel="1" android:drawable="@drawable/process_2" />  ?4:     <item android:maxLevel="2" android:drawable="@drawable/process_3" />  ?5:     <item android:maxLevel="3" android:drawable="@drawable/process_4" />  ?6: </level-list>

<!--CRLF-->

2. For creating this special type of notification, you will need a RemoteView to change the default contentView that Notification class uses so that you don’t have to set the setLatestEventInfo to the notification. The downside here to the Ongoing notification is that you have to have a contentView for any notifications you create. You just can’t do with only the icon on the status bar. Compare it the the battery level icon on the status bar. That seems like a notification

actually, but it is not. And the SDK doesn’t have give us anything to create notifications of that kind. Bottom line is, even if you create an Ongoing notification, when the user pulls down the notification bar, he will see more details about the notification.

Here is the remote view, which contains and image and a message. This example also shows how to create a custom view for your notification. You can attach click events to this remote view and do whatever you want. Launch your activity, start a service, etc.

   1: <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent"  ?2:     xmlns:android="http://schemas.android.com/apk/res/android"    3:     android:layout_height="wrap_content">  ?4:     <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"    5:         android:layout_height="wrap_content" android:src="@drawable/icon">  ?6:     </ImageView>  ?7:     <TextView android:id="@+id/TextView01" android:layout_width="fill_parent"  ?8:         android:layout_height="fill_parent" android:text="Fetching sample data  ?9:         from server" android:gravity="center_vertical" android:textStyle="bold" ?10:         android:textSize="15dip" android:textColor="#FF000000"> ?11:     </TextView> ?12: </LinearLayout>

<!--CRLF-->

3. For setting of the notification, you need to keep a few things in mind. Since we are using a custom view for our notification, we will not set the setLatestEventInfo. And for the animation to work, we will need to change the iconLevel of the notification to values as and when we want. For this example, I am running a thread which changes the iconLevel value every 100ms to animate it faster.

   1:     notification = new Notification();  ?2:     Intent notificationIntent = new Intent();  ?3:     PendingIntent contentIntent = PendingIntent.getActivity(this, (int)  ?4:                             System.currentTimeMillis(),  ?5:                             notificationIntent,    6:                             PendingIntent.FLAG_UPDATE_CURRENT);  ?7:     // We are using the custom view for our notification instead of the  ?8:     // setLatestEventInfo method  ?9:     RemoteViews contentView = new RemoteViews(getPackageName(), ?10:                R.layout.notify);? 11:?   12:     notification.contentView = contentView;??   13:     // Since we do not use the setLatestEventInfo, we have to set the ?14:     // intent like this ?15:     notification.contentIntent = contentIntent;?   16:     // Set the notification type to Ongoing Event, so that you will not ?17:     // see the Clear button for this notification ?18:     notification.flags = Notification.FLAG_ONGOING_EVENT;   19:     // Set the notification icon, which is actually the LevelListDrawable ?20:     // that you have created ?21:     notification.icon = R.drawable.level_list;  22:     // Set the initial iconLevel to 0 ?23:     notification.iconLevel = 0;?   24:     nManager.notify(R.string.app_name, notification);?   25:     // start the thread which updates the iconLevel at regular intervals ?26:     Thread thread = new Thread(this); ?27:     thread.start();

And in the thread’s run method, you just need to keep on changing the iconLevel for the notification.

   1:         if (notification.iconLevel < 3) {  ?2:             notification.iconLevel++;  ?3:         } else {  ?4:             notification.iconLevel = 0;  ?5:         }  ?6:         if (!isStopped) {  ?7:             nManager.notify(R.string.app_name, notification);  ?8:         } else {  ?9:             nManager.cancel(R.string.app_name); ?10:         } ?11:         if (!isStopped) { ?12:             nManager.notify(R.string.app_name, notification); ?13:         } else { ?14:             nManager.cancel(R.string.app_name); ?15:         }

For this example, you don’t need to update the notification since nothing in the custom view changes. If you want to emulate the behavior of the In Call Ongoing notification, then you have to update your notification after changing the contents of the remote view. This is fairly simple once you get the basics working.

Thats it. You have an animated ongoing notification on your status bar which also uses a custom view. You can find the whole source code here. I have created a sample activity with a button to start and stop the notification. This kind of notification is really helpful when you are processing something on a background service and just need to tell the user that the background service is running. No activities/services will be started by clicking on the notification until and unless you handle the click events since we used a custom view here. Once the service stops, you just need to cancel the notification and it will vanish.

You can find the whole source code here. It?s ready to run. You can checkout the project and see the animated ongoing notification right away. Here is a sample video of the app in action.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Python爬虫进阶必备 | 某镜像网站分析 -
  2. 开发者可以在Microsoft Teams的移动应用
  3. Python爬虫进阶必备 | RSA 加密案例解析
  4. 一文彻底搞清 Kafka 的副本复制机制
  5. Python少有人走过的坑
  6. 来自 Facebook 的 Spark 大作业调优经验
  7. 你在享受十一长假时,Python 已悄悄地变了
  8. 编程题三:使用指针来打印数组内容
  9. linux防暴力破解ssh(centos8测试)
  10. 初中学历程序员面试被HR吐槽,初中学历还有