Summary of status bar in Android

Source: Internet
Author: User

The status bar notification is an optional ticker-text information that includes images and notification information, the Android system starts an intent (usually starts an activity ). of course, you can set a ringtone, vibrate, or light to notify users.

The status notification bar should occur when the background service program wants to remind the user of the corresponding events. Android suggests developing this. The background service program should not directly start an activity interface when it needs to interact with the user, your background program should create a status notification. You can select this notification to start an activity interface. Both activity and service can generate notifications, but you will always instantiate them in service. At this time, you need to use two classes: Notification and icationicationmanager.

Notification defines the attributes of notifications, while notificationmanager is used to execute and manage all notifications in the status bar of the Android system. Icationicationmanager can only be instantiated through getsystemservice (), and a notification instance can be passed through notify.

Create your notification:

To create a notification, you need an image that appears in the status bar, with the title in the information (unless you customize the status bar) and an intent (set when the user selects the notification ). There are also a few optional: ticker-text (as for the prompt information, it is the information that appears with the chart when the notification appears, and will automatically disappear with the notification Icon later ), sound, vibration settings, and lighting. Basically instantiate a notification Code as follows:

 1 int icon = R.drawable.notification_icon;        // icon from resources
2 CharSequence tickerText = "Hello"; // ticker-text
3 long when = System.currentTimeMillis(); // notification time
4 Context context = getApplicationContext(); // application Context
5 CharSequence contentTitle = "My notification"; // message title
6 CharSequence contentText = "Hello World!"; // message text
7
8 Intent notificationIntent = new Intent(this, MyClass.class);
9 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
10
11 // the next two lines initialize the Notification, using the configurations above
12 Notification notification = new Notification(icon, tickerText, when);
13 notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

Manage your Notifications:

The following code first gets a reference from icationicationmanager:

1 String ns = Context.NOTIFICATION_SERVICE;
2 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

When you want to pass your notification, you need to use the Y (INT, notification) function. The first parameter here is very important, it is the unique identifier of your notification in this application. When you need to update your notification (maybe there are more than one notification in your status bar) or when the user returns to your application (by the intent you define in the notification), you can determine what action to perform.

It is very easy to clear notifications. You can add "flag_auto_cancel" when defining your notification. You can also call cancel (INT) by passing your notification ID, or you can try cancelall ().

We will not elaborate on how to add sound, vibration, and lights to notifications. The following describes custom notifications.

By default, notifications containing titles and information will appear in your notification window. However, you can also use the remoteviews class to customize a layout to display your notifications.

First, you need to define your notification layout file. You can define a custom_icationication_layout.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView android:id="@+id/textview"
8 android:layout_width="match_parent"
9 android:layout_height="wrap_content"/>
10 <ProgressBar android:id="@+id/progress_bar"
11 style="@android:style/Widget.ProgressBar.Horizontal"
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:max="100"/>
15 </LinearLayout>

At this time, you need to use the remoteviews class for the layout file of infalte. Remoteviews is a view hierarchy that describes a display in another process, and provides some operation methods. After setting remoteview, you can assign it to the contentview attribute of your notification. Below are some of my service classes that generate a thread to automatically update the progressbar value to prompt the user:

1 public class LocalService extends Service {
2 icationicationmanager nmanager = NULL;
3 context = NULL;
4 Notification = NULL;
5 private mythread thread = NULL;
6 Private Static final int hello_id = 1;
7 @ override
8 Public void oncreate (){
9 // todo auto-generated method stub
10 log. E ("localservcie: ------>", "oncreate ");
11 context = getapplicationcontext ();
12 nmanager = (icationicationmanager) Context. getsystemservice (context. icationication_service );
13
14 int icon = R. drawable. ic_launcher;
15 charsequence tickertext = "notification ";
16 long when = system. currenttimemillis ();
17 Notification = new notification ();
18 notification. When = when;
19 notification. tickertext = tickertext;
20 Notification. Icon = icon;
21 super. oncreate ();
22}
23
24 @ override
25 public void onlowmemory (){
26 // todo auto-generated method stub
27 log. E ("localservcie: ------>", "onlowmemory ");
28 thread. stopthread ();
29 nmanager. Cancel (hello_id );
30 Super. onlowmemory ();
31}
32
33 @ override
34 public void ondestroy (){
35 log. E ("localservcie: ------>", "ondestroy ");
36 thread. stopthread ();
37 nmanager. Cancel (hello_id );
38 Super. ondestroy ();
39}
40
41 @ override
42 Public void onrebind (intent ){
43 // todo auto-generated method stub
44 super. onrebind (intent );
45}
46
47 @ override
48 Public int onstartcommand (intent, int flags, int startid ){
49 // todo auto-generated method stub
50 log. E ("localservcie: ------>", "onstartcommand ");
51
52 thread = new mythread ();
53 thread. startthread ();
54 return Super. onstartcommand (intent, flags, startid );
55}
56 @ override
57 public ibinder onbind (intent ){
58 // todo auto-generated method stub
59 return NULL;
60}
61 @ override
62 public Boolean onunbind (intent ){
63 // todo auto-generated method stub
64 return Super. onunbind (intent );
65}
66
67 private void Update (INT value ){
68 remoteviews contentview = new remoteviews (getpackagename (), R. layout. custom_icationication_layout );
69 contentview. settextviewtext (R. Id. textview, "xia Zai Ren Wu 1 ");
70 contentview. setprogressbar (R. Id. progress_bar, 100, value, false );
71 notification. contentview = contentview;
72 intent notificationintent = new intent (context, notificationactivity. Class );
73 pendingintent = pendingintent. getactivity (context, 0, icationicationintent, 0 );
74 notification. contentintent = pendingintent;
75 nmanager. Y (hello_id, notification );
76}
77
78 Class mythread implements runnable {
79 private volatile thread runner;
80
81 public synchronized void startthread (){
82 If (runner = NULL ){
83 runner = new thread (this );
84 runner. Start ();
85}
86}
87
88 public synchronized void stopthread (){
89 If (runner! = NULL ){
90 thread moribund = runner;
91 runner = NULL;
92 moribund. Interrupt ();
93}
94}
95
96 public void run (){
97 while (thread. currentthread () = runner ){
98 // do stuff which can be interrupted if necessary
99 int value = 10;
100 while (value <= 100 ){
101 try {
102 log. E ("LocalService-> thread-> Run ()", "run ");
103 Update (value );
104 thread. Sleep (1000 );
105 value + = 10;
106
107} catch (interruptedexception e ){
108 log. E ("LocalService-> thread-> Run ()", "interruptedexception ");
109 return;
110}
111}
112}
113}
114
115}
116
117
118}

As follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.