android service HttpURLConnection下载网络数据
2017-03-08 19:56
429 查看
import android.app.Notification; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.widget.ImageView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class DownloadService extends Service { public static final String URL_PATH = "DownloadService_URL"; private Notification.Builder notificationBuilder; private NotificationManager notificationManager; private ImageView imageView; private Handler handler = new Handler(){//消息队列,下载完成后发送消息通知下载完成 @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what ==1){ stopSelf(); Bundle bundle = msg.getData(); byte[] data= bundle.getByteArray("data"); Toast.makeText(getApplicationContext(), "下载完成" + data, Toast.LENGTH_LONG).show(); } } }; public DownloadService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); //定义一个通知显示下载进度。 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationBuilder = new Notification.Builder(getApplicationContext()); notificationBuilder.setSmallIcon(R.drawable.nav_task).setTicker("下载").setContentTitle("加载网络数据").setContentText("正在加载中…").setAutoCancel(true); } @Override public int onStartCommand(Intent intent, int flags, int startId) { final String PATH = intent.getStringExtra(URL_PATH); new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); int len = 0; int len_data = 0; byte[] data = new byte[128]; try { URL url = new URL(PATH); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET");//请求模式 connection.setConnectTimeout(8000);//连接等待时间 connection.setReadTimeout(8000);//读取等待时间 InputStream inputStream = connection.getInputStream(); int len_long = connection.getContentLength(); while ((len = inputStream.read(data)) != -1) { arrayOutputStream.write(data, 0, data.length); len_data += len; int progress_value = (int) ((len_data / (float) len_long) * 100);// 进度条刻度计算 notificationBuilder.setProgress(100, progress_value, false); notificationManager.notify(1000, notificationBuilder.build()); } notificationBuilder.setContentText("下载完成!"); notificationManager.notify(1000, notificationBuilder.build()); Bundle bundle = new Bundle(); bundle.putByteArray("data",arrayOutputStream.toByteArray()); Message msg = new Message();//下载完成后将下载的数据通过Message发送到消息队列 msg.setData(bundle); msg.what = 1; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }).start(); return START_REDELIVER_INTENT; //返回值代表了如果服务被系统杀死后启动的方式 //START_REDELIVER_INTENT:系统在onStartCommand()返回后杀死了服务,则将重建服务并用上一个已送过的intent调用onStartCommand()。 } @Override public void onDestroy() { super.onDestroy(); } }
相关文章推荐
- Android HttpURLConnection获取网络下载图片POST请求方式
- android 请求网络数据 HttpURLConnection
- Android用httpURLConnection发送post网络请求并拿到数据
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android 网络编程之HttpURLConnection(conn.getResponseCode() != 200,从url读取写入数据)
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- HttpURLConnection:网络下载数据的工具类
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android中如何使用HttpURLConnection实现GET POST JSON数据与下载图片
- Android - HttpURLConnection下载网络文件,加载网络图片
- android网络编程之HttpUrlConnection的讲解--实现文件断点下载
- Android开发使用HttpURLConnection进行网络编程详解【附源码下载】
- handler、HttpURLConnection、网络数据下载综合使用。
- android重新学_网络3——采用HttpURLConnection提交数据到服务器
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android使用HttpURLConnection请求网络返回JSON数据并解析
- Android通过HttpURLConnection链接到网络,并获取网络数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片