In the QQ project, we have implemented the background operation of the notification bar and sent new message prompts. Generally, we often use two components toast and notification during Message notification. In particular, it is most appropriate to use notification to display important and long-time information. When there is a message notification, the status bar displays the notification icon and text. You can see the notification information through the drop-down status bar. Android, an innovative UI component, has won the praise of users, even Apple began to imitate it. In fact, it is a bit similar to the Windows tray display.
Next, let's take a detailed analysis based on the QQ small project. Let's take a look at the following two:
I. layout file of the notification bar. In our QQ project, when we press the return key in the activity in the friend list, we first mark the program running in the background (which can be a global variable, you can also save it to the sharedpreferenced file), and then send a broadcast. By receiving the broadcast in the service, we will immediately initialize the view in the notification bar running in the background. When a new message arrives, we will not send messages through broadcast (because no activity is running), but directly remind users through the update notification bar and send a notification (with sound and vibration ). The layout file policy_view.xml of the view in the notification bar is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="2dp" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/notify_imageLog" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="5dp" android:src="@drawable/h001" /> <TextView android:id="@+id/notify_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/notify_imageLog" android:paddingLeft="5dp" android:text="name" android:textColor="#000000" android:textSize="20sp" /> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/notify_msg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="15dp" android:text="msg" android:textColor="@color/black" android:textSize="15sp" /> <TextView android:id="@+id/notify_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="right" android:paddingRight="15dp" android:text="time" android:textColor="@color/black" android:textSize="15sp" /> </LinearLayout> </LinearLayout></LinearLayout>
2. initialize the view method in the notification bar. Write a method in getmsgservice to initialize the view in the notification bar:
/*** Create notification */private void setmsgnotification () {int icon = R. drawable. optional Y; charsequence tickertext = ""; long when = system. currenttimemillis (); mnotification = new notification (icon, tickertext, when); // put mnotification in the "running" column. flags = notification. flag_ongoing_event; remoteviews contentview = new remoteviews (mcontext. getpackagename (), R. layout. policy_view); contentview. settextviewtext (R. id. policy_name, util. getname (); contentview. settextviewtext (R. id. notify_msg, "Mobile QQ is running in the background"); contentview. settextviewtext (R. id. notify_time, mydate. getdate (); // specify the personalized view mnotification. contentview = contentview; intent = new intent (this, friendlistactivity. class); pendingintent contentintent = pendingintent. getactivity (mcontext, 0, intent, pendingintent. flag_update_current); // specifies the content intent mnotification. contentintent = contentintent; micationicationmanager. notify (constants. policy_id, mnotification );}
3. The friend list activity returns the broadcast receiver of the key. The user presses the return key to send the broadcast and marks the broadcast. The program runs in the background:
// After receiving the broadcast from the user by pressing the return key, the notification bar private broadcastreceiver backkeyreceiver = new broadcastreceiver () {@ overridepublic void onreceive (context, intent) is displayed) {// todo auto-generated method stubtoast. maketext (context, "QQ enters the background to run", 0 ). show (); setmsgnotification ();}};
4. Update the notification bar using handler. We use handler to process messages and update the notification bar:
// Handlerprivate handler = new handler () {public void handlemessage (Message MSG) {Switch (MSG. what) {Case MSG: int newmsgnum = application. getnewmsgnum (); // obtain newmsgnum ++ from the global variable; // The application is automatically added every time a message is received. setnewmsgnum (newmsgnum); // set it to the global variable tranobject <textmessage> textobject = (tranobject <textmessage>) MSG. getdata (). getserializable ("MSG"); // system. out. println (textobject); If (textob Ject! = NULL) {int form = textobject. getfromuser (); // where the message comes from string content = textobject. getObject (). getmessage (); // The message content chatmsgentity entity = new chatmsgentity ("", mydate. getdateen (), content,-1, true); // messagedb. savemsg (Form, entity); // save it to the database // update the notification bar int icon = R. drawable. policy_newmessage; charsequence tickertext = form + ":" + content; long when = system. currenttimemillis (); mnotification = new notification (icon, tickertext, when); mnotification. flags = notification. flag_no_clear; // sets the default sound mnotification. defaults | = notification. default_sound; // set the vibration (vibrate permission is required) mnotification. defaults | = notification. default_vibrate; mnotification. contentview = NULL; intent = new intent (mcontext, friendlistactivity. class); pendingintent contentintent = pendingintent. getactivity (mcontext, 0, intent, 0); mnotification. setlatesteventinfo (mcontext, util. getname () + "(" + newmsgnum + "new message)", content, contentintent);} micationicationmanager. notify (constants. notify_id, mnotification); // It takes effect only after a notification. Break; default: Break ;}}};
4. Listen to messages. We listen to the messages received by the receiving thread to determine whether the program is running in the background. If it is in the background, we directly send the messages to handler. If not, this message is sent through broadcasting. Therefore, when you press the return key to enter the background, make a flag to indicate that the program runs in the background, I am saving it in the sharedpreferenced file. In fact, it can be saved to the global variables of the application:
In. setmessagelistener (New messagelistener () {@ overridepublic void message (tranobject MSG) {// system. out. println ("getmsgservice:" + MSG); If (util. getisstart () {// if it is running in the background, update the notification bar; otherwise, send the broadcast to activityif (MSG. getType () = tranobjecttype. message) {// only process the text message type // system. out. println ("receive new message"); // sends the message object to handler to process message = handler. obtainmessage (); message. what = MSG; message. getdata (). putserializable ("MSG", MSG); handler. sendmessage (Message) ;}} else {intent broadcast = new intent (); broadcast. setaction (constants. action); broadcast. putextra (constants. msgkey, MSG); sendbroadcast (broadcast); // send the received message in the form of broadcast }}});
Background message processing is implemented in this way. If you have any suggestions or questions, please leave me a message. Thank you.