One of the important Android components, Service Introduction and Learning (1)

Source: Internet
Author: User

 

In Android, asynchronous processing requires important Service and Handler components. Today, let's first learn about the Service components,

The method for inter-process communication and bindService () in Android Service is explained next time.

1: first, let's take a look at the explanation of the Service concept in the document:

Service is a component in the Android system. Its importance can be comparable to Activity, but there are also significant differences. Activity can interact with users, but the Service can only run in the background, cannot directly interact with users;

When we exit the application, the Service will still run in the background, and the process will not end. Pay special attention to the Service we need to register in the service tag of the AndroidManifset. xml file.

Then we have two methods to start the Service: (①: Context. startService () and ②: Context. bindService ()

2: where the Service needs to be used:

The following two examples are given in the document:

The most important thing we think of is that when playing a player, we may need to listen to songs and do other things. At this time, we will exit the player application. If the Service is not applicable, when we exit the application, we cannot listen to songs. For example, we need to obtain data through the network, because the speed of getting data through the network is relatively slow, in this case, we can use the Service to obtain updates in the background, and send back the obtained data over a period of time, instead of letting the application open every time I want to obtain the data;

3: Service and Activity communication:

The data at the backend of the Service must be displayed on the front-end Activity,

Note that services, like other application objects, run in the main thread of their hosting process

The main Service, like other application objects, runs on the main thread of the application process.

4: Service Startup Mode:

I. context. startService ()

2. context. bindService ();

Before using the Service, you must use the <service android: name = ". service Class name"/> In AndroidMainfest. xml for registration.

5: how to use the Service (startService is my main learning today)

Step 1: Write a class that inherits the Service class

Step 2: register in the AndroidMainfest. xml file

Step 3: startService ()

 

 

 

6: lifecycle:

First, let's look at the runtime diagram of the declaration period.

 

 

 

①: Context. startService () will start the Service through several methods.

At startup: context. startService () ---> onCreate () ---> onStartCommand ()

At the time of destruction: ontext. stopService () ---> onDestroy ();

If the Service is not running, the onCreate () method is called first, and then onStartCommand () is called ();

If the Service is already running, only onStartCommand () and onStartCommand () of the Service are called. The method may be called multiple times.

②:

Start Time: context. bindService () ---> onCreate () ---> onBind ();

At the time of destruction: onUnibind () ---> onDestroy ()

OnBind () will return an IBind interface instance to the client. At this time, the client can call the Service method, but at this time the Activity and Service are bound together.

This means that if the Activity exits, the Service also exits.

 

 

Next is the instance Demo:

 

Method of inheriting Service:

 

Import android. app. Service;

Import android. content. Intent;

Import android. OS. IBinder;

 

Public class ServiveTest extends Service {

 

@ Override

Public IBinder onBind (Intent intent ){

// TODO Auto-generated method stub

Return null;

}

 

@ Override

Public void onCreate (){

System. out. println ("onCreat -------> ");

Super. onCreate ();

}

 

@ Override

Public void onDestroy (){

// TODO Auto-generated method stub

System. out. println ("onDestroy -------> ");

Super. onDestroy ();

}

 

@ Override

Public int onStartCommand (Intent intent, int flags, int startId ){

System. out. println ("onStartCommand -------> ");

Return super. onStartCommand (intent, flags, startId );

}

 

}

 

 

Today, I first learned how to use context. startService () for services. Next time, I will learn how to use context. bindService () to communicate with processes.

From the cold night --- the journey of cainiao Learning

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.