In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the use of Android service Service". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Foreword:
Android services are an important part of developing Android applications. Unlike the active Activity, the service runs in the background, the service has no interface, and the life cycle is very different from the active Activity. By using the service, we can implement some background operations, such as trying to load a web page from a remote server. Services can help us to implement multitasking in Android.
We already know that Android activities can be started, stopped, and destroyed when system resources are too low, and the service is designed to implement tasks with longer execution time. Android services can be launched from an active Activity, or from a broadcast receiver and other services.
It must be noted that using the service does not require the automatic creation of a new thread, so if we implement a simple logic in the service, it does not take a long time to process, and we do not need to run it in a separate thread. But if we want to implement complex logic that takes a long time to deal with, we must create a new thread to execute, otherwise running the service on the main thread may lead to ANR problems.
1. Service purpose
The service is suitable for two purposes:
Achieve multitasking
Activate Inter-Process-Communication (IPC) interprocess communication
A typical example of the first situation is the need to download data from a remote server, in which case we can have the application interact with the user at the same time and start working in the background, while the user can continue to use the application and send a message to the user when the service is complete.
In the second case, we can "share" some common functions through services so that different applications can reuse them. For example, suppose we have an e-mail service that we want to share among several applications so that we don't have to rewrite the same code. In this case, we can use IPC to expose the service, which exposes the interface "remotely" and is called by other applications.
Here is a simple service that inherits Service.
2. Inherit Servicepublic class TestService extends Service {@ Override public IBinder onBind (Intent arg0) {return null;}}
Services have a lifecycle, and some of these callback methods can be implemented:
Public class TestService extends Service {@ Override public void onCreate () {super.onCreate ();} @ Override public void onDestroy () {super.onDestroy ();} @ Override public int onStartCommand (Intent intent, int flags, int startId) {return super.onStartCommand (intent, flags, startId);} @ Override public IBinder onBind (Intent arg0) {return null }}
The method onCreate is called only once when the service is created. If the service is already running, this method will not be called. Instead of calling it directly, the operating system OS calls this method.
OnStartCommand is the most important method, because when it is called, we need to start the service. In this approach, we need to pass the intention to the service we are running so that we can exchange some information with the service. The logic implemented in this method can be executed directly in this method, and if execution is time-consuming, we need to create a thread. As you can see, this method requires us to return an integer as a result. This integer indicates how the service is handled by the operating system.
START_STICKY: with this return value, if OS kills our service, it will recreate it, but the intended Intent sent to the service will no longer be passed, in which case the service is always running.
START_NOT_STICKY: if OS kills the service, it will not be created until the client explicitly activates the onStart command
START_REDELIVER_INTENT: it is similar to START_STICKY, and the intention is passed back to the service.
OnDestroy is called by the operating system when the service is destroyed.
The service needs to be configured in Manifest.xml:
3. Start and stop services
As we know, the service must first be started and finally stopped after completing its task. We can launch it from the active Activity, and we can use Intent to pass some information to the service. Suppose we have two buttons, one to start and one to stop service.
BtnStart.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent I = new Intent (MainActivity.this, TestService.class); i.putExtra ("name", "SurvivingwithAndroid"); MainActivity.this.startService (I);}}) BtnStop.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent I = new Intent (MainActivity.this, TestService.class); MainActivity.this.stopService (I);}})
The running effect is as follows:
IntentService
As we mentioned earlier, the service runs on the main thread, so we have to be very careful in executing some logic in this service. It must be taken into account that if this logic is a blocking operation, or if it takes a long time to complete, it will cause an ANR problem. In this case, the logic is transferred to a separate thread, which means that we must create the thread and run it in the onStartCommand method. There is another type of derivative service called IntentService that simplifies these operations. This class is useful when we do not need to process multiple requests at the same time. This class creates a worker thread to handle different requests
The functions are as follows:
Create a detached thread to process the request
Create a request queue and pass an Intent message
Create a default implementation of onStartCommand
Stop the service when all requests are processed
Public class TestIntentService extends IntentService {public TestIntentService () {super ("TestIntentService");} @ Override protected void onHandleIntent (Intent intent) {}}
We implement the logic in onHandleIntent, regardless of the fact that the job takes half a day or more, because this method runs in a separate thread.
4. Start the service automatically
If we want to start the smartphone when it is powered on, we first create a broadcast receiver, listen to the event, and then start the service.
Public class BootBroadcast extends BroadcastReceiver {@ Override public void onReceive (Context ctx, Intent intent) {ctx.startService (new Intent (ctx, TestService.class));}}
Configuration in Manifest.xml:
This is the end of the content of "what is the purpose of Android Service Service". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.