学习了安卓多线程之后极客班老师要求做一个下载器,当然老师本身已经提供了非常详细的代码,我就只把代码详细分析,稍微修改下。
下载器页面由EditText、按钮和进度条组成,当点击按钮时根据EditText里的网址来进行下载。在获取下载资源时会得到资源的总大小,用当前已经下载的资源的大小与总大小相处即可得到下载进度。
下面是代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String GEEK_BAND = "GeekBand";
private TextView mTextView;
private Button mDownloadButton;
private ProgressBar mProgressBar;
private EditText mEditText;
public static final String TAG = MainActivity.class.getSimpleName();
private Handler mHandler = new DownloadHandler(this);
public TextView getmTextView(){
return mTextView;
}
public ProgressBar getmProgressBar(){
return mProgressBar;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text_view);
mDownloadButton = (Button) findViewById(R.id.download_button);
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
mEditText = (EditText) findViewById(R.id.url_edit);
mDownloadButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.download_button:
new Thread(new Runnable() {
@Override
public void run() {
String apkurl = mEditText.getText().toString();
download(apkurl);
}
}).start();
break;
}
}
private void download(String apkUrl){
try {
URL url = new URL(apkUrl);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
int contentLength = urlConnection.getContentLength();
String downloadFoldersName = Environment.getExternalStorageDirectory()+ File.separator +GEEK_BAND +File.separator;
File file = new File(downloadFoldersName);
if (!file.exists()){
file.mkdir();
}
String fileName = downloadFoldersName +"test.apk";
File apkFile = new File(fileName);
if (apkFile.exists()){
apkFile.delete();
}
int downloadSize = 0;
byte[] bytes = new byte[1024];
int length = 0;
OutputStream outputStream = new FileOutputStream(fileName);
while ((length = inputStream.read(bytes))!= -1){
outputStream.write(bytes,0,length);
downloadSize += length;
int progress = downloadSize*100/contentLength;
Message message = mHandler.obtainMessage();
message.obj = progress;
message.what = 0;
mHandler.sendMessage(message);
inputStream.close();
outputStream.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static class DownloadHandler extends Handler{
public final WeakReference<MainActivity> mActivity;
public DownloadHandler(MainActivity activity) {
mActivity = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
MainActivity activity = mActivity.get();
switch (msg.what){
case 0:
int progress = (int) msg.obj;
activity.getmProgressBar().setProgress(progress);
activity.getmTextView().setText("progress"+progress);
if (progress == 100){
Toast.makeText(activity,"download success",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
}
当然需要INTERNET和外部读写权限,要记得在安卓的MAinfest文件里加上。