One android service (start service)

Source: Internet
Author: User

 

We have two methods (start and bind) to start a Service. The Service life cycle of each method is different. This post mainly describes start service.

There are only three stages in its lifecycle: onCreate, onStartCommand (replacing the original onStart method), and onDestroy. For example:

 

 

Note the following:

① If the caller directly exits without calling stopService, The started Service will run in the background until its stopService method is called or it calls the stopSelf method by itself.

② When a service is not created, the system first calls the onCreate () method of the service and then calls the onStartCommand () method. If the service has been created before the startService () method is called, The onStartCommand () method is called directly. That is to say, calling the startService () method multiple times does not cause multiple service creation. In addition, no matter how many times the service is started, you only need to stop the corresponding service once.

③ Specific operations should be placed in onStartCommand ()

See the following:

 

The four buttons in are for the operation of the same Service. In this case, go to application and you can see the list of "Running Service" as follows:

 

 

 

Click the above button (startservice 1 and 2 respectively). The result is as follows:

 

We can see that the onCreate () method is called only when the service is created for the first time.

Now, use the "Return key" to return to the main interface, and then go to the running service of the application to view it. You can see the following:

 

From this figure, we can see that although the Activity is finished, the service started by it is still running in the background.

In this case, re-open the application, and then directly click the stop service 1 and 2 buttons twice (you do not need to click the start service button again), you can:

 

From this figure, we can see that the onDestroy () method is called only when the service is stopped for the first time.

In this case, go to the running service of the application and check the following and find that the service has been stopped:

 

 

Some source code is attached below (For details, refer to the attachment ):

Java code

// RESPONSE event of the four buttons

 

Private OnClickListener btnListener = new OnClickListener (){

@ Override

Public void onClick (View v ){

Switch (v. getId ()){

Case R. id. startSer1:

UpdateLog ("Start Service 1 pressed ");

// Start the service (if you want to transmit data, you can encapsulate it into the intent)

StartService (intent );

Break;

Case R. id. startSer2:

UpdateLog ("Start Service 2 pressed ");

StartService (intent );

Break;

Case R. id. stopSer1:

UpdateLog ("Stop Service 1 pressed ");

// Stop the service

StopService (intent );

Break;

Case R. id. stopSer2:

UpdateLog ("Stop Service 2 pressed ");

StopService (intent );

Break;

Default:

Break;

}

}

};

 

Java code

// Service implementation

Public class MyService extends Service {

Private static final String TAG = "MyService ";

 

@ Override

Public void onCreate (){

Super. onCreate ();

MyServiceActivity. updateLog (TAG + "----> onCreate ()");

}

 

@ Override

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

MyServiceActivity. updateLog (TAG + "----> onStartCommand ()");

Return START_STICKY;

}

 

@ Override

Public IBinder onBind (Intent intent ){

MyServiceActivity. updateLog (TAG + "----> onBind ()");

Return null;

}

 

@ Override

Public void onDestroy (){

Super. onDestroy ();

MyServiceActivity. updateLog (TAG + "----> onDestroy ()");

}

}

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.