Android Message Push Implementation program

Source: Internet
Author: User


1. Message push mechanism
server-side needs to be passive for active, informing customers 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 hung ...", exactly.
Smart-Aleck, will bring a little smart, some people like to hate.
2. Standalone process
Regardless of whether the program is running, 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, a
Global, stand-alone process is opened for this service, and a private process for this application is opened for this service if you start with ":". To give a specific example, we created a new
application to create the main process Com.cnblogs.tianxia, then:
The code is as follows:
<!– The following creates a global com.cnblogs.tianxia.message standalone process –>
<service android:name= ". Service.messageservice" Android: label= "message Push" android:process= ". Messages"/>
<!– or –>
<!– The following creates an application-private com.cnblogs.tianxia: The standalone process of the message –>
<service android:name= ". Service.messageservice" android:label= "Messages Push" android:process= ": Message "/>
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 users and click to 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 an activity that clicks on the jump and is responsible for processing view details.
We call it in other activity:
The code is as follows:
Boolean ismessagepush = true;//set to False if not turned on;
...
if (ismessagepush) {
StartService (new intent (this, messageservice.class))
};

Run it:
4. Stop service
1 StopService (New Intent (myactivity.this,messageservice.class));
2 Setmessagepush (FALSE);//Set configuration file or database flag to false
Run, stop the service, but unexpectedly did not stop, what's going on? Is the code written wrong?
There is nothing wrong with the code when we stop the service, but we don't stop the process and exit the thread.
5. Exit thread
Practice has proved that the stop () method of thread is not reliable. But we have other options.
The programmer is God in front of the code.
There are two ways to exit a thread.
The first method, which forces the exit.
//kills the process where the thread is located and naturally quits
System.exit (0);
The second method, setting IsRunning to False.
//Earlier on isrunning this flag, set to false, the execution of the thread jumps out of the while loop, and then naturally ends
Messagethread.isrunning = false;
to combine, we overload the OnDestroy () method in Messageservice as follows:
The following code:
@override
public void OnDestroy () {
System.exit (0 );
//Or, select 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.