Code for message push mechanism using App in Android

Source: Internet
Author: User

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.
When registering a service in androidmanifest. xml, there is an android: process attribute. If this attribute starts with ".",
Global independent process. If it starts with ":", an independent process private to this application is enabled for this service. For example, we have created a new
Application, created the main process com. cnblogs. tianxia, so: Copy codeThe Code is as follows: <! -- An independent process of com. cnblogs. tianxia. message will be created below -->
<Service android: name = ". service. messageservice" android: label = "message push" android: process = ". message"/>
<! -- Or -->
<! -- A private com. cnblogs. tianxia: independent process of message will be created below -->
<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 {

// Obtain the message thread
Private messagethread = null;

// Click to View Details
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 null;
}

@ Override
Public int onstartcommand (intent, int flags, int startid ){
// Initialization
Messagenotification = new notification ();
Messagenotification. icon = r. drawable. icon;
Messagenotification. tickertext = "New message ";
Messagenotification. defaults = notification. default_sound;
Messagenotificatiomanager = (icationicationmanager) getsystemservice (context. icationication_service );

Messageintent = new intent (this, 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 );
}

/**
* Obtain messages from the server
*
*/
Class messagethread extends thread {
// Running status, which is useful for the next step of www.3ppt.com
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 the notification bar
Messagenotification. setlatesteventinfo (messageservice. this, "New message", "Obama announced that benla
Brother login hung up! "+ Servermessage, messagependingintent );
Messagenotificatiomanager. Y (messagenotificationid, messagenotification );
// After each notification, the notification id increments to avoid overwriting the message.
Messagenotificationid ++;
}
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
}

/**
* Here we use this method as the server demo, for example only
* @ Return refers to the message to be pushed by the server. Otherwise, if it is null, It is not pushed.
*/
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:Copy codeThe Code is as follows: 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
1 stopservice (new intent (myactivity. this, messageservice. class ));
2 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 in which the thread is located, and exit naturally.
2 system. exit (0 );
Method 2: Set isrunning to false.
View sourceprint? 1 // As mentioned earlier, the isrunning flag is set to false, and the thread execution jumps out of the while loop, and ends naturally.
Dropped
2 messagethread. isrunning = false;
Here, we reload ondestroy () in messageservice as follows:Copy codeThe Code is as follows: @ override
Public void ondestroy (){
System. exit (0 );
// Alternatively, we recommend that you use system. exit (0) to clean the process exit.
// Messagethread. isrunning = false;
Super. ondestroy ();
}

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.