1、设置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET"/>
2、业务代码
package com.example.esri.app04.network; import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; public class MainActivity extends AppCompatActivity { private File apkFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void downloadAPK(View v){ final ProgressDialog dialog = new ProgressDialog(this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); //准备用于保存apk文件的file对象 放在sd卡里面 apkFile = new File(getExternalFilesDir(null), "update.apk"); //启动分线程下载数据,显示下载进度 new Thread(new Runnable() { @Override public void run() { try{ //1 得到链接对象 String path = "http://192.168.0.184:8080/storage.apk"; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(10000); connection.connect(); int responseCode = connection.getResponseCode(); if(responseCode == 200){ //设置dialog最大进度 dialog.setMax(connection.getContentLength()); InputStream is = connection.getInputStream(); FileOutputStream fos = new FileOutputStream(apkFile); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1){ fos.write(buffer, 0, len); dialog.incrementProgressBy(len); } fos.close(); is.close(); } connection.disconnect(); //主线程,移除dialog,启动安装 runOnUiThread(new Runnable() { @Override public void run() { dialog.dismiss(); installAPK(); } }); }catch (Exception e){ e.printStackTrace(); } } }).start(); } /** * 启动安装apk */ private void installAPK() { Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE"); intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); startActivity(intent); } }
使用异步任务下载
package com.example.esri.l05.handler; import android.app.ProgressDialog; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class AsyncTaskActivity extends AppCompatActivity { private File apkFile; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_async_task); } public void downloadAPK(View v){ //启动异步任务处理 new AsyncTask<Void, Integer, Void>() { //1主线程显示提示试图 @Override protected void onPreExecute() { dialog = new ProgressDialog(AsyncTaskActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); apkFile = new File(getExternalFilesDir(null), "update.apk"); } //2分线程,联网请求 @Override protected Void doInBackground(Void... params) { try{ //1 得到链接对象 String path = "http://192.168.0.184:8080/storage.apk"; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(10000); connection.connect(); int responseCode = connection.getResponseCode(); if(responseCode == 200){ //设置dialog最大进度 dialog.setMax(connection.getContentLength()); InputStream is = connection.getInputStream(); FileOutputStream fos = new FileOutputStream(apkFile); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1){ fos.write(buffer, 0, len); //在分线程发布进度 publishProgress(len); // dialog.incrementProgressBy(len); } fos.close(); is.close(); } connection.disconnect(); }catch (Exception e){ e.printStackTrace(); } return null; } //3主线程,更新界面 @Override protected void onPostExecute(Void aVoid) { dialog.dismiss(); installAPK(); } //在主线程中更新进度 (在publishProgress之后执行) @Override protected void onProgressUpdate(Integer... values) { dialog.incrementProgressBy(values[0]); } }.execute(); } /** * 启动安装apk */ private void installAPK() { Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE"); intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); startActivity(intent); } }