Android USES App to implement the message push mechanism of the code

  • 2020-05-05 11:51:30
  • OfStack

1. Message push mechanism
The
server side needs to change from passive to active, informing the client of information that some developers consider important, whether the application is running or closed.
I came up with a phrase: don 'est call me,i will call you!
qq today displays a dialog in the lower right corner: "Obama announces bin laden's death..." Exactly.
If you are too smart for your own good, you will get a little smart. Some people like it, others hate it.
2. Separate process
We need to be able to notify the customer whether the program is running or not, and we need a backend service for a separate process.
We need a backend service for a separate process.
When service is registered in androidmanifest.xml, there is an android:process attribute, and if the attribute begins with ".", an
is opened for this service A global independent process that starts with ":" and starts a private independent process for the service. For example, we created a new
application, creates the main process com.cnblogs.tianxia, then:
 
<!-- The next step is to create a global one com.cnblogs.tianxia.message Independent process --> 
<service android:name=".service.messageservice" android:label=" Being pushed " android:process=".message" /> 
<!-- or --> 
<!-- The next step is to create an application private com.cnblogs.tianxia:message Independent process --> 
<service android:name=".service.messageservice" android:label=" Being pushed " android:process=":message" /> 
 Instead of creating a global one, this article selects the second option, which is to create a separate process that is currently private to the application.  
3. Notify the user and click to view  
public class messageservice extends service { 

// Get message thread  
private messagethread messagethread = null; 

// Click to view  
private intent messageintent = null; 
private pendingintent messagependingintent = null; 

// Notification board 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) { 
// Initialize the  
messagenotification = new notification(); 
messagenotification.icon = r.drawable.icon; 
messagenotification.tickertext = " The 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 the thread  
messagethread = new messagethread(); 
messagethread.isrunning = true; 
messagethread.start(); 

return super.onstartcommand(intent, flags, startid); 
} 

/** 
*  Get the message from the server side  
* 
*/ 
class messagethread extends thread{ 
// Running state, www.3ppt.com The next step is useful  
public boolean isrunning = true; 
public void run() { 
while(isrunning){ 
try { 
// rest 10 minutes  
thread.sleep(600000); 
// Get server message  
string servermessage = getservermessage(); 
if(servermessage!=null&&!"".equals(servermessage)){ 
// Update notification  
messagenotification.setlatesteventinfo(messageservice.this," The new message "," Obama announces , The la  
 The den brothers are dead !"+servermessage,messagependingintent); 
messagenotificatiomanager.notify(messagenotificationid, messagenotification); 
// After each notification, notification id Increment to avoid message overwriting  
messagenotificationid++; 
} 
} catch (interruptedexception e) { 
e.printstacktrace(); 
} 
} 
} 
} 

/** 
*  This method here is the server demo , as an example only  
* @return  Returns the message to be pushed by the server or not if it is empty  
*/ 
public string getservermessage(){ 
return "yes!"; 
} 
} 


messageactivity is the activity of click jump, which is responsible for handling the detailed information.
Let's call
in the other activity
 
boolean ismessagepush = true;// Set to if not on false; 
... 
if(ismessagepush){ 
startservice(new intent(this, messageservice.class)) 
}; 

Run
4. Discontinue
1 stopservice(new intent(myactivity.this,messageservice.class));
2 setmessagepush (false); // set flag to false
in the configuration file or database Run, stop service, but unexpectedly did not stop, why? Is the code wrong?
There is nothing wrong with the code, it is that we stopped the service, but did not stop the process, exit the thread.
5. Exit thread
Practice has proved that stop() method of thread is not reliable. But we have other ways.
In front of the code, the programmer is god.
There are two ways to exit a thread.
The first way is to force the exit.
// kills the process in which the thread resides, and
exits 2 system.exit(0);
Second, set isrunning to false.
view sourceprint? 1 // the isrunning flag was mentioned earlier, and when it was set to false, the execution of the thread jumped out of the while loop and ended
naturally Off
2 messagethread.isrunning = false;
To summarize, we overloaded the ondestroy() method in messageservice as follows:
 
@override 
public void ondestroy() { 
system.exit(0); 
// Or, one of the two, recommended system.exit(0) , so that the process exits cleaner  
//messagethread.isrunning = false; 
super.ondestroy(); 
} 


Related articles: