Android uses service implementation to download network images to the SDK card

Source: Internet
Author: User

1  Packagecom.example.myapp5;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.widget.Button;8 /**9 * Android uses service to download picture functionTen  * @authorshaobn One * @date 2015-9-14 A * @packege com.example.myapp5.MyApp5 -  */ -  Public classMainactivityextendsActivity { the     Privatebutton button; - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -Button = (Button) This. Findviewbyid (r.id.button1); +Button.setonclicklistener (NewView.onclicklistener () { A              at @Override -              Public voidOnClick (View arg0) { -                 //TODO auto-generated Method Stub -Intent Intent =NewIntent (mainactivity. This, MyService.class); - StartService (intent); -             } in         }); -     }     to}
1  Packagecom.example.myapp5;2 3 ImportJava.io.File;4 ImportJava.io.FileOutputStream;5 6 ImportOrg.apache.http.HttpResponse;7 Importorg.apache.http.client.HttpClient;8 ImportOrg.apache.http.client.methods.HttpGet;9 Importorg.apache.http.impl.client.DefaultHttpClient;Ten Importorg.apache.http.util.EntityUtils; One  A ImportAndroid.app.Service; - Importandroid.content.Intent; - Importandroid.os.Environment; the ImportAndroid.os.Handler; - ImportAndroid.os.IBinder; - ImportAndroid.os.Message; - ImportAndroid.widget.Toast; +  -  Public classMyServiceextendsService { +     Private Final StaticString Imagepah = "Https://www.baidu.com/img/bd_logo1.png"; A @Override at      Public voidonCreate () { -         //TODO auto-generated Method Stub -         Super. OnCreate (); -     } - @Override -      Publicibinder onbind (Intent arg0) { in         //TODO auto-generated Method Stub -         return NULL; to     } +  - @Override the      Public intOnstartcommand (Intent Intent,intFlagsintStartid) { *         //TODO auto-generated Method Stub $         FinalHandler Handler =NewHandler () {Panax Notoginseng              -              Public voidhandlemessage (android.os.Message msg) { the                 if(msg.what==1){ +Toast.maketext (Getapplicationcontext (), "Off", 1). Show (); A stopself (); the                 }         +             }; -              $         }; $         NewThread (NewRunnable () { -              - @Override the              Public voidrun () { -                 //TODO auto-generated Method StubWuyiHttpClient HttpClient =Newdefaulthttpclient (); theHttpGet HttpGet =NewHttpGet (imagepah); -HttpResponse HttpResponse =NULL; WuFile File =environment.getexternalstoragedirectory (); -File file2 =NULL; AboutFileOutputStream fos =NULL; $                 Try { -HttpResponse =Httpclient.execute (httpget); -                 if(Httpresponse.getstatusline (). Getstatuscode () ==200) { -                     byte[] num =Entityutils.tobytearray (Httpresponse.getentity ()); A                     if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) { +File2 =NewFile (file, "/movies/a.jpg")); the file2.createnewfile (); -FOS =NewFileOutputStream (file2); $ fos.write (num); theMessage message =Message.obtain (); theMessage.what = 1; the handler.sendmessage (message); the                     } -                 } in                      the}Catch(Exception e) { theE.printstacktrace ();//Todo:handle Exception About}finally{ the                     if(fos!=NULL){ the                         Try { the fos.close (); +}Catch(Exception E2) { -                             //Todo:handle Exception the e2.printstacktrace ();Bayi                         } the                     } the                  -                 } -                  the             } the }). Start (); the         return Super. Onstartcommand (Intent, flags, startid); the     } - @Override the      Public voidOnDestroy () { the         //TODO auto-generated Method Stub the         Super. OnDestroy ();94     } the}



Android uses service implementation to download network images to the SDK card

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.