Android Learning Series (7)-App Message notification mechanism

Source: Internet
Author: User

Some people say that programmers are quiet, but I don't fully agree that the noise of programmers is hidden behind the code and behind the program.
This article is a must-have knowledge for Android Developers. It is specially compiled and summarized for everyone. It is not perfect but useful. 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:Copy codeWe 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 viewCopy code

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

Copy codeRun:
<Ignore_js_op> 4. Stop the service. Copy codeAfter 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. Copy code

Method 2: Set isRunning to false.

Copy code

Here, we reload onDestroy () in MessageService as follows:

Copy code

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!

Author: humble world

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.