Android 利用Service实现下载网络图片至sdk卡

 1 package com.example.myapp5;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.Button;
 8 /**
 9  * Android 利用service实现下载图片功能
10  * @author shaobn
11  * @date 2015-9-14
12  * @packege com.example.myapp5.MyApp5
13  */
14 public class MainActivity extends Activity {
15     private Button button;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         button = (Button) this.findViewById(R.id.button1);
21         button.setOnClickListener(new View.OnClickListener() {
22             
23             @Override
24             public void onClick(View arg0) {
25                 // TODO Auto-generated method stub
26                 Intent intent = new Intent(MainActivity.this,MyService.class);
27                 startService(intent);
28             }
29         });
30     }    
31 }
 1 package com.example.myapp5;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 
 6 import org.apache.http.HttpResponse;
 7 import org.apache.http.client.HttpClient;
 8 import org.apache.http.client.methods.HttpGet;
 9 import org.apache.http.impl.client.DefaultHttpClient;
10 import org.apache.http.util.EntityUtils;
11 
12 import android.app.Service;
13 import android.content.Intent;
14 import android.os.Environment;
15 import android.os.Handler;
16 import android.os.IBinder;
17 import android.os.Message;
18 import android.widget.Toast;
19 
20 public class MyService extends Service {
21     private final static String IMAGEPAH = "https://www.baidu.com/img/bd_logo1.png";
22     @Override
23     public void onCreate() {
24         // TODO Auto-generated method stub
25         super.onCreate();
26     }
27     @Override
28     public IBinder onBind(Intent arg0) {
29         // TODO Auto-generated method stub
30         return null;
31     }
32 
33     @Override
34     public int onStartCommand(Intent intent, int flags, int startId) {
35         // TODO Auto-generated method stub
36         final Handler handler = new Handler(){
37             
38             public void handleMessage(android.os.Message msg) {
39                 if(msg.what==1){
40                     Toast.makeText(getApplicationContext(), "关闭了", 1).show();
41                     stopSelf();
42                 }        
43             };
44             
45         };
46         new Thread(new Runnable() {
47             
48             @Override
49             public void run() {
50                 // TODO Auto-generated method stub
51                 HttpClient httpClient = new DefaultHttpClient();
52                 HttpGet httpGet = new HttpGet(IMAGEPAH);
53                 HttpResponse httpResponse = null;
54                 File file = Environment.getExternalStorageDirectory();
55                 File file2 = null;
56                 FileOutputStream fos = null;
57                 try {
58                 httpResponse =httpClient.execute(httpGet);
59                 if (httpResponse.getStatusLine().getStatusCode()==200) {
60                     byte[] num = EntityUtils.toByteArray(httpResponse.getEntity());
61                     if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
62                         file2 = new File(file, "/Movies/a.jpg");
63                         file2.createNewFile();
64                         fos = new FileOutputStream(file2);
65                         fos.write(num);
66                         Message message = Message.obtain();
67                         message.what = 1;
68                         handler.sendMessage(message);
69                     }
70                 }
71                     
72                 } catch (Exception e) {
73                     e.printStackTrace();// TODO: handle exception
74                 }finally{
75                     if(fos!=null){
76                         try {
77                             fos.close();
78                         } catch (Exception e2) {
79                             // TODO: handle exception
80                             e2.printStackTrace();
81                         }
82                     }
83                 
84                 }
85                 
86             }
87         }).start();
88         return super.onStartCommand(intent, flags, startId);
89     }
90     @Override
91     public void onDestroy() {
92         // TODO Auto-generated method stub
93         super.onDestroy();
94     }
95 }

 


           
     

posted @ 2015-09-14 22:49  邻家小书童  阅读(492)  评论(0编辑  收藏  举报