Android------service intentservice

Source: Internet
Author: User




First, there are two problems with the service itself:

1.Service does not start a new thread specifically, the service is in the same process as the application it resides in.

2.Service is also not a new thread, so time-consuming tasks should not be handled in the service.



Intentservice just made up for these two deficiencies:

The Intentservice can handle the service of an asynchronous request.

Intentservice will use the queue to manage the request intent whenever the client code starts Intentservice through intent requests Intentservice
The intent is added to the queue and a new worker thread is opened to process the intent. It will stop automatically when it is finished, without having to stop it manually.


Features of Intentservice:
Intentservice creates a separate worker thread to handle all intent requests
Intentservice creates a separate worker thread to handle the code implemented by the Onhandleintent method, so the developer does not need to deal with multithreading issues
When all request processing is complete, Intentservice automatically stops without calling the Stopself method to stop the service
Provides a default implementation for the service's Onbind method, which returns null by default
Provides a default implementation for the service's Onstartcommand method, which adds intent to the queue.


Therefore, when inheriting intentservice, do not need Onbind Onstartcommand method, as long as implement Onhandleintent method can




public class Intentservicetest extends activity{@Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void StartService (View source) {//create intentintent intent = new Intent (this, myservice.class) required to start the service;// Start Servicestartservice (intent);} public void Startintentservice (View source) {//create intentservice that needs to be started intentintent intent = new Intent (this, Myintentservice.class);//Start intentservicestartservice (intent);}}

The two time processing methods of activity above activate MyService and Myintentservice respectively.



Myservice.java:


public class MyService extends service{@Overridepublic ibinder onbind (Intent Intent) {return null;} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//The time-consuming task within this method may cause ANR (application not Responding) exception long endTime = System.currenttimemillis () + 20 * 1000; System.out.println ("OnStart"), while (System.currenttimemillis () < EndTime) {synchronized (this) {try{wait (endTime -System.currenttimemillis ());} catch (Exception e) {}}}toast.maketext (Getapplicationcontext (), "time-consuming task execution complete", 1). Show (); SYSTEM.OUT.PRINTLN ("---time-consuming task execution---"); return start_sticky;}}


Myintentservice.java:


public class Myintentservice extends Intentservice{public Myintentservice () {Super ("Myintentservice");} Intentservice uses a separate thread to execute the code for the method @overrideprotected void Onhandleintent (Intent Intent) {//the method can perform any time-consuming tasks, such as downloading files, etc., Here just let the thread pause for 20 seconds long endTime = System.currenttimemillis () + 20 * 1000; System.out.println ("OnStart"), while (System.currenttimemillis () < EndTime) {synchronized (this) {try{wait (endTime -System.currenttimemillis ());} catch (Exception e) {}}}system.out.println ("---time-consuming task execution completed---"); Toast.maketext (Getapplicationcontext (), "time-consuming task execution complete", 0). Show ();}}





Android------service intentservice

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.