Code _android of using app to implement message push mechanism in Android

Source: Internet
Author: User
1. Message push mechanism
The server side needs to be passive, to inform the customer of information that some developers deem important, regardless of whether the application is running or shutting down.
I thought of a word: Don t call Me,i would call you!
QQ today in the lower right corner pop-up a dialog box: "Obama announced that Osama bin Laden ...", it is so.
Smart-Aleck, you will bring a little clever, some people like to hate.
2. The process of independence
Whether the program is running or not, we have to be able to notify the customer that we need a background service for a separate process.
We need a background service for a separate process.
When registering a service in Androidmanifest.xml, there is a android:process property, if the property is "." Begins, the service opens a
A global stand-alone process that opens an independent process for this service to this application if it starts with ":". As a concrete example, we created a new
Application, created the main process Com.cnblogs.tianxia, then:
Copy Code code as follows:

<!--will create a global com.cnblogs.tianxia.message independent process-->
<service android:name= ". Service.messageservice" android:label= "message Push android:process=". Messages "/>
<!--or-->
<!--a standalone process that applies a private com.cnblogs.tianxia:message is created below-->
<service android:name= ". Service.messageservice" android:label= "message Push" android:process= ": Messages"/>
We don't need to build a global, this article chooses the second scenario to create an independent process that currently applies private.
3. Notify the user and click View
public class Messageservice extends service {

Getting message threads
Private Messagethread 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 Notificationmanager Messagenotificatiomanager = null;

Public IBinder Onbind (Intent Intent) {
return null;
}

@override
public int Onstartcommand (Intent intent, int flags, int startid) {
Class
Messagenotification = new notification ();
Messagenotification.icon = R.drawable.icon;
Messagenotification.tickertext = "new Message";
Messagenotification.defaults = Notification.default_sound;
Messagenotificatiomanager = (Notificationmanager) getsystemservice (Context.notification_service);

Messageintent = new Intent (this, messageactivity.class);
Messagependingintent = pendingintent.getactivity (this,0,messageintent,0);

Open Thread
Messagethread = new Messagethread ();
Messagethread.isrunning = true;
Messagethread.start ();

Return Super.onstartcommand (Intent, flags, Startid);
}

/**
* Get messages from server side
*
*/
Class Messagethread extends thread{
Running state, www.3ppt.com the next step has a big use
public Boolean isrunning = true;
public void Run () {
while (isrunning) {
try {
Take a 10-minute break.
Thread.Sleep (600000);
Get Server Messages
String servermessage = Getservermessage ();
if (servermessage!=null&&! "". Equals (ServerMessage)) {
Update Notification Bar
Messagenotification.setlatesteventinfo (Messageservice.this, "New News"), "Obama announced that Ben pulls
Brother Hung Up! " +servermessage,messagependingintent);
Messagenotificatiomanager.notify (Messagenotificationid, messagenotification);
After each notification, the notification ID increment, to avoid the message coverage
messagenotificationid++;
}
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

/**
* Here is the server demo for this method, just for example
* @return return to the server to push the message, otherwise, if it is empty, do not push
*/
public string Getservermessage () {
return "yes!";
}
}


Where Messageactivity is a click to jump activity, responsible for processing view details.
We call it in other activity:
Copy Code code as follows:

Boolean Ismessagepush = true;//set to False if not open;
...
if (Ismessagepush) {
StartService (This, messageservice.class) (new intent)
};

Run it:
4. Stop service
1 StopService (New Intent (myactivity.this,messageservice.class));
2 Setmessagepush (FALSE);//setting Flag to False in the configuration file or database
Run, stop the service, but unexpectedly did not stop, what's going on? Is the code written wrong?
The code is not wrong, the fault is that we stopped the service, but did not stop the process, exit the thread.
5. Exit thread
Practice has proved that the stop () method of thread is not reliable. But we have other options.
In front of the code, the programmer is God.
There are two ways to exit a thread.
The first method, force exit.
Kill the process where the thread is located, and naturally quit.
2 system.exit (0);
The second method, set IsRunning to False.
View sourceprint?1//preceded by the IsRunning flag, set to False, the thread's execution jumps out of the while loop and ends naturally.
Off.
2 messagethread.isrunning = false;
In combination, we overload the OnDestroy () method in Messageservice as follows:
Copy Code code as follows:

@override
public void OnDestroy () {
System.exit (0);
Alternatively, choose one, recommend the use of System.exit (0), so that the process exits cleaner
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.