Introduction to basic Service overview and FAQs in Android Development

Source: Internet
Author: User

A Service is an application component that can run for a long time in the background without providing a user interface. Other application components can start a Serivce, which will continue to run in the background even if the user switches to another application. In addition, a component can be bound to a service to interact with it, or even execute inter-process communication (IPC ). For example, a Serivice may process online transactions, play music, execute file I/O, or interact with a content provider for all backgrounds.

Where does Serivice run? This is a common problem. By default, the Service runs in the hosting process of the main thread (when it does not create its own thread and does not run in a separate process ). What does this mean? If your service will do any CPU-intensive or blocking operations (such as MP3 playback or network), you should create a service job in a new thread. By using a separate thread, the application that reduces risks does not respond to "(ANR) errors and the main thread of the application can still focus on user interaction activities and UI interaction.

When should we use the Service and when should we use the Thread? The Service is a simple component that can be run in the background, even if the user does not interact with the application, it can also run. Therefore, if you create a service, you can use it only for interface interaction.

If you need to execute work outside your main thread, but only run in the interaction between the user and the application, you should not create a new thread, not a Service. For example, if you want to play some music, but only when your activity is running, you can create a thread to run it at onCreate () and onStart, then stop in onStop (). You can also consider using AsyncTask or HandlerThread instead of the traditional Thread class.

Will the Service be closed by itself? When a Service is started, it has an independent life cycle. Its components and services can run in the background indefinitely, even if its components are destroyed. Therefore, the Service should stop itself when it works by calling stopSelf (), or another component can prevent it from calling stopService ().

Like an Activity, a Service has its own lifecycle callback method. You can monitor Service status changes and perform work at appropriate time. The following framework demonstrates each lifecycle method.

public class ExampleService extends Service {    int mStartMode;       // indicates how to behave if the service is killed    IBinder mBinder;      // interface for clients that bind    boolean mAllowRebind; // indicates whether onRebind should be used    @Override    public void onCreate() {        // The service is being created    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        // The service is starting, due to a call to startService()        return mStartMode;    }    @Override    public IBinder onBind(Intent intent) {        // A client is binding to the service with bindService()        return mBinder;    }    @Override    public boolean onUnbind(Intent intent) {        // All clients have unbound with unbindService()        return mAllowRebind;    }    @Override    public void onRebind(Intent intent) {        // A client is binding to the service with bindService(),        // after onUnbind() has already been called    }    @Override    public void onDestroy() {        // The service is no longer used and is being destroyed    }}



OnStartCommand ()

The system calls this method as another component, such as an Activity, which requires the service to be started by calling startService (). After this method is executed, the Service starts and can run in the background. If you implement this, you must call stopSelf () or stopService () after stopping the Service (). (Note: If you only want to provide binding, you do not need to implement this method ).

OnBind ()

The system calls this method when another component wants to bind a Service (such as executing RPC) by calling bindService (). When you implement this method, you must provide an interface for the customer to communicate with the service by returning an IBinder. You must implement this method, but if you do not want to allow binding, we should return null.

OnCreate ()

When the system calls the Service for the first time, this method is called only once.

In addition, like Acitivity, the Service must be declared in the configuration file, as shown below

<manifest ... >  ...  <application ... >      <service android:name=".ExampleService" />      ...  </application></manifest>

Next, I will share with you in detail the specific Service use cases. Please stay tuned.



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.