您的位置:首页 > 理论基础 > 计算机网络

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 网络 数据
相关文章推荐