话不多说先看效果图
思路:
1.AlertDialog弹框提示
2.ProgressDialog进行下载进度提示
3.AsyncTask进行网络读取数据
代码:
先看MainActivity
“` /* * 测试工具类的下载文件工具,本demo是直接运行 * */ public class
MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //这是你要下载的文件url String url="http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk"; //调用工具类方法 Utils.downFile(MainActivity.this,url,"xxx.apk"); }
} “`
再看utils工具类
/* 作者:桃先森 * 时间:2017/5/25 * 类用途:自定义封装工具类,供大家使用 *
思路:AlertDialog弹框提示,ProgressDialog进行下载进度提示 ** 静态方法介绍 *
参数一:上下文 * 参数二:下载的文件url * 参数三:保存在本地的名字(带文件类型 例如 aaa.apk) */public class Utils {
public static void downFile(final Context context, final String url, final String filename) { final AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提醒"); builder.setMessage("有新版本,请更新"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { ProgressDialog progressDialog; @Override public void onClick(DialogInterface dialog, int which) { progressDialog = new ProgressDialog(context); progressDialog.setMessage("正在更新,请稍等"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setIndeterminate(true); progressDialog.show(); startAsyntask(context,progressDialog,url,filename); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { builder.create().dismiss(); } }); builder.show(); } //启动Asyntask的方法 private static void startAsyntask(final Context context, final ProgressDialog progressDialog, String url, final String filename) { new AsyncTask<String, Integer, String>() { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); progressDialog.setIndeterminate(false); progressDialog.setMax(100); progressDialog.setProgress(values[0]); if (values[0] == 100) { Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show(); progressDialog.dismiss(); } } @Override protected void onPostExecute(String string) { super.onPostExecute(string); } @Override protected String doInBackground(String... params) { String param = params[0]; try { URL url = new URL(param); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != 200) { return "链接错误"; } //获得内容长度 int contentLength = connection.getContentLength(); InputStream inputStream = connection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("/sdcard/"+filename); byte[] buffer = new byte[1024]; int len = 0; int total = 0; while ((len = inputStream.read(buffer)) != -1) { if (isCancelled()) { inputStream.close(); } else { total += len; publishProgress((total * 100 / contentLength)); fileOutputStream.write(buffer, 0, len); } } return null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }.execute(url); }
}
小小工具类,大家可直接使用,如有帮助到您,那是我的荣幸!你的支持就是我的动力,以后会不定式更新能帮助大家的Demo!期待下次在见,我是桃先森我为狸菇凉代言–>_<