Android Self-study road--service and Intentservice

Source: Internet
Author: User


A) mainactivity part of the code
1 PackageCom.example.cms.intentservice;2 3 Importandroid.content.Intent;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.Button;8 9 Public classMainactivityextendsappcompatactivity {Ten PrivateButton StartService; One PrivateButton Startintentservice; A - @Override - protected voidonCreate (Bundle savedinstancestate) { the Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -Startservice=(Button) Findviewbyid (r.id.service); -Startintentservice=(Button) Findviewbyid (r.id.intentservice); +Startservice.setonclicklistener (NewView.onclicklistener () { - @Override + Public voidOnClick (View v) { A StartService (); at } - }); -Startintentservice.setonclicklistener (NewView.onclicklistener () { - @Override - Public voidOnClick (View v) { - Startintentservice (); in } - }); to } + Public voidStartService () { -Intent Intent =NewIntent ( This, MyService.class); the StartService (intent); * } $ Public voidStartintentservice () {Panax NotoginsengIntent intent=NewIntent ( This, Myintentservice.class); - StartService (intent); the } +}

B) MyService part of the code
1  PackageCom.example.cms.intentservice;2 3 ImportAndroid.app.Service;4 Importandroid.content.Intent;5 ImportAndroid.os.IBinder;6 Importandroid.support.annotation.Nullable;7 8 /**9 * Created by CMS on 2016/3/28.Ten  */ One  Public classMyServiceextendsService { A @Nullable - @Override -      Publicibinder onbind (Intent Intent) { the         return NULL; -     } -  - @Override +      Public intOnstartcommand (Intent Intent,intFlagsintStartid) { -         //use thread pauses to simulate time-consuming tasks pausing for 20 seconds +         //Normal service execution will block the main thread so executing this method will cause a ANR exception A         LongEndtime=system.currenttimemillis () +20*1000; atSystem.out.println ("OnStart"); -          while(System.currenttimemillis () <endTime) { -             synchronized( This){ -                 Try { -Wait (endtime-System.currenttimemillis ()); -}Catch(interruptedexception e) { in e.printstacktrace (); -                 } to             } +         } -SYSTEM.OUT.PRINTLN ("---time-consuming task execution complete----"); the         returnStart_sticky; *     } $}

C) Myintentservice part of the code

 PackageCom.example.cms.intentservice;ImportAndroid.app.IntentService;Importandroid.content.Intent;ImportAndroid.content.IntentSender;/*** Created by CMS on 2016/3/28.*/ Public classMyintentserviceextendsIntentservice { PublicMyintentservice () {Super("Myintentservice"); } @Overrideprotected voidonhandleintent (Intent Intent) {//Intentservice will use a separate thread to execute the code for this method//Does not block the main thread so there is no ABR exception        LongEndtime=system.currenttimemillis () +20*1000; System.out.println ("Onhandleintent");  while(System.currenttimemillis () <endTime) {            synchronized( This){                Try{Wait (endTime-System.currenttimemillis ()); } Catch(interruptedexception e) {e.printstacktrace (); }}} System.out.println ("Time-consuming task execution complete"); }}

Android Self-study road--service and Intentservice

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.