Traditional Culture Encyclopedia - Weather inquiry - How to turn Android service process into foreground process
How to turn Android service process into foreground process
Service has been running in the background almost all the time, and has been working hard silently. However, the system priority of services is still relatively low. When the system is out of memory, services running in the background can be recycled. If you want the service to run all the time and not be recycled because of insufficient system memory, you can consider using the foreground service.
The biggest difference between foreground service and ordinary service is that it always has a running icon displayed in the status bar of the system. You can see more detailed information after you drop down the status bar, much like the effect of notification. Of course, sometimes you may use the front desk service not only to prevent the service from being recycled, but also for some special needs, such as ink weather. Its service will always display the current weather information in the system status bar and update the weather data in the background.
So let's take a look at how to create a front desk service, which is actually not complicated, as shown below:
This is just the code to modify the onCreate () method in the service. As you can see, we first create a notification object, and then call its setLatestEventInfo () method to initialize the layout and data of the notification, and set click notification here to open the MainActivity. Then call the startForeground () method to turn MyService into a foreground service, and the picture of the notification will be displayed.
Now run the program again, the service will start in the mode of the previous service, and a banner icon will pop up in the system status bar. You can see the details of the notification after you drop down the status bar.
- Previous article:How to judge the occurrence of earthquakes according to natural phenomena?
- Next article:How to check the limit number?
- Related articles
- What sentences describe blowing?
- Why is it so hot in Henan?
- What should I pay attention to when planting a lawn like Sijiqing Guolingcao in my family yard?
- Excuse me, which dynasty was the great migration of farmers in Daliushu, Shanxi? How many years ago?
- What customs should I pay attention to when traveling to Thailand? What's the temperature there in mid-March? Is it hot?
- Introduction of Characters in the Classic Chapter of Water Margin
- Chongqing Yunyang weather forecast data
- /kloc-what to wear in 0/0-22 degrees?
- What's the climate like in Lanzhou
- Thunderstorm teaching plan