Android interview questions -- Service, android -- service

Source: Internet
Author: User

Android interview questions -- Service, android -- service

1. Is the Service executed in main thread and time-consuming operations performed in the service?
By default, if no process is displayed, the Service and activity are running in the main thread (main UI thread) of the process where the current app is located.
The service cannot perform time-consuming operations (network requests, copy databases, and large files). You can configure the process where the service is executed in the configuration file so that the service can be executed in another process.

<service    android:name="com.baidu.location.f"    android:enabled="true"    android:process=":remote" ></service>

2. How to bind an Activity to a Service and start its own Service in the Activity?
Activity is bound to the service through bindService (Intent Service, ServiceConnection conn, int flags). When the binding is successful, the Service will send the proxy object to conn through callback, in this way, we get the Service proxy object provided by the Service.
You can use startService and bindService methods to start a Service in an Activity. Generally, to obtain the Service object of a Service, you must use the bindService () method, such as music player and third-party payment. If you only want to enable a background task, you can use the startService () method.

3. Describe the Service Lifecycle
The Service has the binding mode and the non-binding mode, and the mixed use mode of the two modes. Different use methods have different lifecycles.
Non-binding mode: The methods executed when startService is called for the first time are onCreate () and onStartCommand (). When the Service is disabled, the onDestory method is called.
Binding mode: When bindService () is used for the first time, onUnbind () and onDestory () are executed when onCreate () and onBind () are unbound ().

The lifecycle of a service. There are two different paths from when it is created to when it is destroyed:

A started service: the enabled service is created by calling startService () through other components. This kind of service can run infinitely. You must call the stopSelf () method or other components to stop it by calling the stopService () method. When the service is stopped, the system will destroy it.

A bound service: The bound service is created when other components (A customer) Call bindService. The customer can communicate with the service through an IBinder interface.

You can use the unbindService () method to close the connection. One service can be bound to multiple customers at the same time. When multiple customers are unbound, the system will destroy the service.

These two paths are not completely separated. That is, you can bind a service that has been enabled by calling startService.

For example, a background music service may be enabled by calling the startService () method. Later, you may want to control the player or obtain information about the current song. You can use bindService () bind an activity to a service. In this case, stopService () or stopSelf () cannot actually stop this service unless all customers are unbound.

The lifecycle diagram of the Service is shown below to help you remember

4. What is IntentService? What are the advantages?

We usually only use the Service. Maybe IntentService is the first time that most of the students have heard of it. After reading the following introduction, I believe that you are no longer new. If you still don't understand it, you can honestly say that you have never used it or don't understand it during the interview. Not all questions need to be answered.

1. Introduction to IntentService

IntentService is a subclass of a Service, which provides additional functions than a common Service. First, there are two problems with the Service itself:
The Service does not start a separate process. The Service and its application are in the same process. The Service is not a new thread. Therefore, it is not necessary to directly process time-consuming tasks in the Service;

Ii. IntentService features

An independent worker thread is created to process all Intent requests;
An independent worker thread is created to process the Code implemented by the onHandleIntent () method, without the need to handle multithreading issues. After all requests are processed, IntentService stops automatically without calling stopSelf () method To stop the Service; provide default implementation for the onBind () of the Service, return null; provide default implementation for the onStartCommand of the Service, and add the request Intent to the queue;
5. What is the relationship between Activity, Intent, and Service?

They are all the most frequently used classes in Android development. Activity and Service are one of the four Android components. Both of them are ContextWrapper subclasses of the Context class, so they can be considered sibling. However, they have their own skills. Activity is responsible for display and interaction of user interfaces, and Service is responsible for processing background tasks. The Activity and Service can transmit data through Intent, so Intent can be considered as a communication messenger.

6. Is the Service and Activity in the same thread?

By default, the same app is in the same Thread, main Thread (UI Thread ).

7. Can I have a toast in the Service?

Yes. There is a condition for the pop-up toast that there must be a Context, and the Service itself is a subclass of the Context, so it is completely possible to play the toast in the Service. For example, after we complete the download task in the Service, we can play a toast to notify the user.

8. What is a Service and its lifecycle. What are the startup methods and differences between services and how to disable services?
In the Service life cycle, the callback method is less than the Activity method, only onCreate, onStart, onDestroy, onBind and onUnbind.
There are usually two ways to start a Service. Their impact on the Service lifecycle is different.
1. Use startService
The Service goes through onCreate to onStart and is in the running state. The onDestroy method is called during stopService. If the caller directly exits without calling stopService, the Service will continue to run in the background.
2. bindService
The Service runs onCreate and then calls onBind. At this time, the caller and Service are bound together. When the caller exits, Srevice will call the onUnbind-> onDestroyed method.
The so-called binding together will survive. The caller can also call the unbindService method to stop the service. At this time, Srevice will call the onUnbind-> onDestroyed method.
What happens if these methods are intertwined? One principle is that the onCreate method of the Service will be called only once, that is, no matter how many times startService and bindService, the Service will be created only once.

If bind is started, the onStart method of Service is run directly. If start is started, the onBind method is run directly during bind.

If the service calls the bindService during running, and then calls the stopService, the service will not call the onDestroy method, and the service will stop, so it can only call the UnbindService, the service will be destroyed. If a service is called multiple times after it is started through startService, the service will call the onStart method multiple times. If you call stopService multiple times, the service only calls the onDestroyed method once.

If a service is called multiple times after it is started through bindService, the service calls the onBind method only once. If unbindService is called multiple times, an exception is thrown.

9. Can I perform network operations on the service life cycle method onstartConmand? How do I perform network operations in the service?

You can perform network operations directly in the Service and in the onStartCommand () method.

Related Article

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.