根据指定url自动下载apk并且调出安装界面.并且显示下载进度和网速,遍历指定路径的指定格式文件.

qq_25039477 2016-10-14 10:49:05
根据指定url自动下载apk并且调出安装界面.并且显示下载进度和网速,遍历指定路径的指定格式文件.把自己写的和大家分享一下 免得新手走错路 大神勿喷
...全文
224 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_25039477 2016-10-14
  • 打赏
  • 举报
回复
我本来想把demo传上来的 奈何不知道怎么传 只有把代码全部放上来了 需要的就看看 不喜欢的就右上角 4.0以上的机子亲测可用 对了 别忘记加权限 别忘记加权限 别忘记加权限
qq_25039477 2016-10-14
  • 打赏
  • 举报
回复
package com.example.autodownload; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.R.layout; import android.app.Activity; import android.content.Intent; import android.net.TrafficStats; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; /** * 1、根据指定url自动下载apk并且调出安装界面.并且显示下载进度和网速 * 2、遍历指定路径的指定格式文件. * @author xxxxxxH * @since 2016年10月14日 10:42:22 * @version 1.0.0 * */ public class MainActivity extends Activity { private ProgressBar pro; private TextView tv; private int filelength; private int downfile; private File file2; private long lastspeed = 0; private long lasttime = 0; private List<File> listfile = new ArrayList<File>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pro = (ProgressBar) findViewById(R.id.pro); tv = (TextView) findViewById(R.id.tv); tv2 = (TextView) findViewById(R.id.tv2); // name(); // new Timer().schedule(task, 0, 500);//新建任务 0秒启动 每500毫秒执行一次 try { String path = Environment.getExternalStorageDirectory().getCanonicalPath(); File[] file = new File(path).listFiles(); getfile(file); for (int i = 0; i < listfile.size(); i++) { Log.i("xxxxxxH", listfile.get(i).getName() + ""); } } catch (IOException e) { e.printStackTrace(); } } /** * 遍历文件夹 * @param files 文件夹路径 * @author xxxxxxH * @since 2016年10月14日 10:28:47 * */ private void getfile(File[] files){ for (int i = 0; i < files.length; i++) { File file = files[i]; if (file.isDirectory()) { File[] file2 = file.listFiles(); getfile(file2); }else if (isok(file)) { listfile.add(file); } } } /** * 判断文件夹下是否包含指定文件 * @param file 文件 * @return true/false * @author xxxxxxH * @since 2016年10月14日 10:28:37 * */ private boolean isok(File file){ int start = file.getName().lastIndexOf("."); int end = file.getName().length(); if (start != -1) { String indexname = file.getName().substring(start + 1, end); if (indexname.equals("jpg")) { return true; } } return false; } TimerTask task = new TimerTask() { @Override public void run() { speed(); } }; /** * 根据指定url下载apk * @author xxxxxxH * @since 2016年10月14日 10:29:55 * */ private void name() { new Thread(new Runnable() { @Override public void run() { try { String path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/xxxxxxH"; File file = new File(path); if (!file.exists()) { file.mkdir(); } //新建下载文件夹,如果不存在 则创建↑↑↑↑↑ String filename = path + "/xh3.apk"; file2 = new File(filename); if (!file2.exists()) { file2.createNewFile(); } //新建下载文件,如果不存在 则创建↑↑↑↑↑ URL url = new URL("http://www.one-gis.cn:809/data/APK/yidong.apk"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //简历连接↑↑↑↑↑ if (connection.getResponseCode() == 200) { InputStream inputStream = connection.getInputStream();//获取输入流 FileOutputStream outputStream = new FileOutputStream(file2);//打开输出流准备写入 byte[] buffer = new byte[1024]; filelength = connection.getContentLength();//获取文件长度 while (downfile < filelength) {//downfile:已下载字节 int len = inputStream.read(buffer);//读取 if (len == -1) { break; } outputStream.write(buffer, 0, len);//写入 downfile += len; Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); //向主线程发送消息 更新UI } connection.disconnect(); inputStream.close(); outputStream.close(); //写入完毕 断开连接 关闭流↑↑↑↑↑ Message msg = new Message(); msg.what = 2; handler.sendMessage(msg); }else { Message msg = new Message(); msg.what = -1; handler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } public Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: pro.setMax(filelength);//设置进度条最大值 pro.setProgress(downfile);//设置进度条进度 int x = downfile * 100 / filelength;//计算下载进度 tv.setText(x+"%"); break; case 2: if (downfile == filelength) { Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); installAPK(); }else { Toast.makeText(MainActivity.this, "下载出错", Toast.LENGTH_SHORT).show(); } break; case -1: Toast.makeText(MainActivity.this, "下载出错", Toast.LENGTH_SHORT).show(); break; case 3: tv2.setText(msg.obj.toString()); break; default: break; } }; }; private TextView tv2; /** * 自动调出安装程序 * @author xxxxxxH * @since 2016年10月14日 10:34:12 * */ private void installAPK(){ Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file2), "application/vnd.android.package-archive"); startActivity(intent); } /** * 计算下载速度 * @author xxxxxxH * @since 2016年10月14日 10:34:43 * */ private void speed(){ long nowspeed = getTotalRxBytes();//1.获取当前下载量 long nowtime = System.currentTimeMillis();//2.获取当前时间 long speed = (nowspeed - lastspeed) * 1000 / (nowtime - lasttime);//3.下载量之差/时间之差 = 下载速度 lastspeed = nowspeed; lasttime = nowtime; //4.计算完之后 上一次的下载量 == 当前下载量 上一次的时间 == 当前时间 Message msg = new Message(); msg.what = 3; msg.obj = String.valueOf(speed) + "kb/s"; handler.sendMessage(msg); } /** * 获取当前下载量 * @author xxxxxxH * @since 2016年10月14日 10:35:17 * */ private long getTotalRxBytes() { return TrafficStats.getUidRxBytes(getApplicationInfo().uid)==TrafficStats.UNSUPPORTED ? 0 :(TrafficStats.getTotalRxBytes()/1024);//转为KB } }

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧