In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to understand the self-startup and maintenance of the Android SERVICE background service process, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.
Service components are often encountered in android development, and they often act as background services that need to be kept running all the time, responsible for handling necessary (and shameful) tasks. Some security software, such as 360th, will have the function of ending the process, and will be killed if it does not maintain the Service.
How to maintain the running state of Service is now to be explained, the core is to use the system broadcast of ANDROID, which will not be affected by other software to trigger their own program to check the running status of Service, if killed, get up again.
The system broadcast I use is Intent.ACTION_TIME_TICK, which is sent every minute, and we can check the running status of Service every minute, and restart Service if it has been finished.
Here are the specific codes and considerations:
1. The use of Intent.ACTION_TIME_TICK
We know that the registration of broadcast has static registration and dynamic registration, but this system broadcast can only be used through dynamic registration. That is, you cannot receive this broadcast by registering in manifest.xml, you can only register through the registerReceiver () method in the code.
Register broadcasts with ThisApp extends Application:
IntentFilter filter = newIntentFilter (Intent.ACTION_TIME_TICK); MyBroadcastReceiver receiver = new MyBroadcastReceiver (); registerReceiver (receiver, filter)
In the onReceive of the broadcast receiver MyBroadcastReceiver extends BroadcastReceiver
If (intent.getAction () .equals (Intent.ACTION_TIME_TICK)) {/ / check Service status}
2. Check and start Service
Boolean isServiceRunning = false; ActivityManager manager = (ActivityManager) ThisApp.getContext (). GetSystemService (Context.ACTIVITY_SERVICE); for (RunningServiceInfo service: manager.getRunningServices (Integer.MAX_VALUE)) {if ("so.xxxx.WidgetUpdateService" .equals (service.service.getClassName () / / Service's class name {isServiceRunning = true;}} if (! isServiceRunning) {Intent I = new Intent (context, WidgetUpdateService.class); context.startService (I);}
Another topic, the boot of Service.
The implementation is similar to the one above, which starts Service by monitoring the system broadcast of boot. But in fact, you will not boot if you have done the above check, because you will start Service through the above program in a minute or two. The code is as follows:
If (intent.getAction (). Equals (Intent.ACTION_BOOT_COMPLETED)) {Intent I = new Intent (context, LogService.class); context.startService (I);} on how to understand the start-up and maintenance of the Android SERVICE backend service process has been shared here. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.