Android Face question 2

Source: Internet
Author: User

1, whether the service is executed in the main thread, is the service able to perform time-consuming operations?
By default, if the process that Servic is running is not displayed, Service and activity are running in the main thread (UI main thread) of the process in which the current app resides.
Service does not perform time-consuming operations (network requests, copy databases, large files) special cases, you can configure the process of service execution in the manifest file, let the service execute in another process

< Service     Android:name = "COM.BAIDU.LOCATION.F"     android:enabled= "true"    android:process= ": Remote"> </ Service >

2, how the activity and service binding, how to start their own service in the activity?
Activity is bound to the service via Bindservice (Intent service, serviceconnection conn, int flags), and when the binding succeeds, the service passes the proxy object through a callback to Conn, so we get the service proxy object provided by the service.
The Service can be started in Activity through the StartService and Bindservice methods. In general, if you want to get service objects then you will definitely need to pass the Bindservice () method, such as music player, third party payment, etc. You can use the StartService () method only to open a background task.

3, please describe the Service life cycle
The Service has binding and unbound modes, as well as a mixed use of the two modes. Different ways of using life cycle methods are also different.
Unbound mode: When the first call to StartService executes the method followed by OnCreate (), Onstartcommand (), and the Ondestory method is called when the Service is closed.
Binding mode: The first time Bindservice (), the execution of the method for OnCreate (), Onbind () Unbind the time will be executed Onunbind (), Ondestory ().
The life cycle of a service, from the time it is created, to the time it is destroyed, can have two different paths:
A started service: The service that is opened is created by calling StartService () from another component. This service can run indefinitely, and you must call the Stopself () method or other component to call the StopService () method to stop it. When the service is stopped, the system destroys it.
A bound service: the service being bound is created when another component (a customer) calls Bindservice (). Customers can communicate via a IBinder interface and service.
Customers can use the Unbindservice () method to close this connection. A service can be bound to multiple customers at the same time, and the service is destroyed when multiple customers are unbound.
These two paths are not completely separate. That is, you can bind to a service that has been invoked with StartService () and is turned on.
For example, a background music service may be turned on by calling the StartService () method, and later, perhaps the user wants to control the player or get some information about the current song, and can bind an activity and service through Bindservice (). In this case, StopService () or stopself () will not actually stop the service unless all the customers are unbound.
The life cycle diagram of the Service is shown below to help remember:

4. What is Intentservice? What are the advantages?
We usually only use the service, perhaps Intentservice for most of the students is the first time to hear. Then look at the following introduction to believe that you are no longer unfamiliar. If you still don't understand, then you can honestly say that you haven't used or understood the interview. Not all the questions need to be answered.
I. Introduction of Intentservice
Intentservice is a subclass of service that adds additional functionality to the normal service. First look at the Service itself with two questions:
The service does not start a separate process specifically, the service is in the same process as the application it is in, and the service is not a new thread, so time-consuming tasks should not be handled directly in the service;
Ii. Characteristics of Intentservice
A separate worker thread is created to handle all Intent requests;
A separate worker thread is created to handle the code implemented by the Onhandleintent () method, eliminating the need to handle multithreading issues, and when all request processing is complete, Intentservice stops automatically without calling the Stopself () method to stop the Service; The service's Onbind () provides the default implementation, returns NULL, provides a default implementation for the service's Onstartcommand, and adds the request Intent to the queue;

5, talk about Activity, Intent, Service is what relationship
They are all the most frequently used classes in Android development. Activity and Service are among the four components of Android. Both of them are subclasses of the subclass of the Context class, so they can be considered contextwrapper. But the two brothers each have their own skills, Activity is responsible for the user interface display and interaction, Service responsible for background task processing. Data can be passed through Intent between activity and Service, so Intent can be thought of as a messenger of communication.

6. Service and Activity on the same thread?
For the same app, by default in the same thread, main thread (UI thread).

7. Can I play toast in the Service?
OK. One of the conditions of the toast is that there is a context, and the service itself is a subclass of the context, so it is perfectly possible to play toast in the service. For example, we can play a toast notification user after completing the download task in the Service.

8, what is the Service and describe its life cycle. What are the service startup methods, what is the difference, and how do I deactivate the service?
In the Service lifecycle, the callback method is less than Activity, only onCreate, OnStart, Ondestroy,onbind, and Onunbind.
There are usually two ways to start a service, and their impact on the service life cycle is different.
1. by StartService
The Service will go through onCreate to OnStart and then be in a running state, StopService call the OnDestroy method. If the caller exits directly without calling StopService, the Service will run in the background.
2. by Bindservice
The service runs OnCreate and then calls Onbind, which is the time the caller and the service are bound together. When the caller exits, Srevice calls the Onunbind->ondestroyed method.
The so-called binding together is a common survival. The caller can also stop the service by calling the Unbindservice method, when Srevice calls the Onunbind->ondestroyed method.
It is important to note that if these methods are intertwined, what will happen? One principle is that the OnCreate method of Service will only be called once, that is, how many times you startservice and Bindservice,service are created only once.
If BIND is first, then start will run the Service's OnStart method directly, if first start, then bind will run the Onbind method directly.
If Bindservice is called during service run time, then call StopService, service will not call OnDestroy method, service stop will not drop, can only call Unbindservice, Ser Vice will be destroyed. If a service calls StartService multiple times after StartService is start, the service calls the OnStart method multiple times. If you call StopService multiple times, the service will only call the Ondestroyed method once.
If a service calls Bindservice multiple times after Bindservice is start, the service will only invoke the Onbind method once. If you call Unbindservice multiple times, an exception will be thrown.

9, in the service life cycle Method Onstartconmand () can perform network operations? How do I perform network operations in the service?
Network operations can be performed directly in the Service, and network operations can be performed in the Onstartcommand () method

Android Face question 2

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.