Android Learning Series (7)-app Message notification mechanism

Source: Internet
Author: User

Someone says,ProgramThe staff is quiet, but I don't fully agree that the noise of programmers is hidden inCodeIt is hidden behind the program.
This articleArticleIt is a must-have knowledge for Android Developers. It is not perfect but useful for everyone.

1. Message push mechanism
The server needs to be changed from passive to active, notifying the customer of information that the developer deems important, whether or not the application is running or disabled.
I thought of a sentence: Don't call me, I will call you!
Today, a dialog box pops up in the lower right corner of QQ: "Obama announces that bin Laden has crashed.
If you are smart, you will be smart. If you like it, you will hate it.

2. Independent Processes
No matter whether the program is running or not, we must be able to notify the customer that we need an independent process Background Service.
We need a background service for an independent process.
In androidmanifest. when registering a service in XML, there is an android: process attribute, if this attribute is ". ", a global independent process is started for this service. If it starts with": ", an independent process private to this application is started for this service. For example, we have created an application and the main process com. cnblogs. Tianxia:

<! -- A global com. cnblogs. tianxia. independent process of message --> <service android: Name = ". service. messageservice "Android: Label =" message push "Android: Process = ". message "/> <! -- Or --> <! -- A private com. cnblogs. tianxia: independent process of message --> <service android: Name = ". service. messageservice "Android: Label =" message push "Android: Process =": Message "/>

We do not need to establish a global one. This article selects the second solution to create an independent process private to the current application.

3. Notify users and click to view

Public class messageservice extends Service {// gets the message thread private messagethread = NULL; // click to view private intent messageintent = NULL; private pendingintent messagependingintent = NULL; // notification bar message private int messagenotificationid = 1000; private notification messagenotification = NULL; private icationicationmanager messagenotificatiomanager = NULL; Public ibinder onbind (intent) {return n Ull ;}@ override public int onstartcommand (intent, int flags, int startid) {// initialize messagenotification = new notification (); messagenotification. icon = R. drawable. icon; messagenotification. tickertext = "New message"; messagenotification. defaults = notification. default_sound; messagenotificatiomanager = (icationicationmanager) getsystemservice (context. notification_service); messageintent = new intent (T His, messageactivity. class); messagependingintent = pendingintent. getactivity (this, 0, messageintent, 0); // enable the thread messagethread = new messagethread (); messagethread. isrunning = true; messagethread. start (); return Super. onstartcommand (intent, flags, startid);}/*** get the message from the server **/class messagethread extends thread {// running status, in the next step, public Boolean isrunning = true; Public void run () {While (isrunning ){ Try {// rest for 10 minutes thread. Sleep (600000); // obtain the server message string servermessage = getservermessage (); If (servermessage! = NULL &&! "". Equals (servermessage) {// update notification bar messagenotification. setlatesteventinfo (messageservice. This, "New message", "Obama announced that bin Laden's brother had crashed! "+ Servermessage, messagependingintent); messagenotificatiomanager. Y (messagenotificationid, messagenotification); // after each notification, the notification ID increments to avoid overwriting messagenotificationid ++;} catch (interruptedexception e) {e. printstacktrace () ;}}}/*** this method is used as the server demo. Only the example * @ return is used to return the message to be pushed by the server. Otherwise, if it is null, do not push */Public String getservermessage () {return "Yes! ";}}

Messageactivity is the activity to jump to. It is responsible for processing and viewing details.
We can call the following in other activities:

 
Boolean ismessagepush = true; // if it is not enabled, it is set to false;... If (ismessagepush) {startservice (new intent (this, messageservice. Class ))};

Run:

4. Stop the service

 
Stopservice (new intent (myactivity. This, messageservice. Class); setmessagepush (false); // set the flag in the configuration file or database to false

After the service is stopped, it is unexpectedly not stopped. Why? Is the code wrong?
The code is correct. if the service is stopped but the process is not stopped, the thread is exited.

5. Exit the thread
Practice has proved that the stop () method of thread is not reliable. But we have other methods.
In front of code, programmers are God.
There are two ways to exit the thread.
Method 1: Force exit.

 
// Kill the process where the thread is located, and naturally exit system. Exit (0 );

Method 2: Set isrunning to false.

 
// The isrunning flag is mentioned earlier. When it is set to false, the thread execution jumps out of the while loop, and messagethread. isrunning = false is naturally ended;

Here, we reload ondestroy () in messageservice as follows:

 
@ Overridepublic void ondestroy () {system. exit (0); // or, either. We recommend that you use system. exit (0), so that the process exits more cleanly // messagethread. isrunning = false; super. ondestroy ();}

Now, messageservice and messageservice can be stopped and exited normally, either manually or forcibly from the task manager.
I think I have already clearly explained the implementation principles of the message push mechanism. If you think it is better, let's say that you can support it!

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.