android 开发app下载

private void downLoadApk()
{
new Thread(new Runnable()
{
@Override
public void run()
{
InstallApk(downLoadFile());
}
}).start();
}


public void InstallApk(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}


public File downLoadFile()
{
File tmpFile = new File(Const.ROOTDIRECTRY);
if (!tmpFile.exists())
{
tmpFile.mkdir();
}
final File file = new File(Const.ROOTDIRECTRY + "/" + Const.XUESHIJIAAPK);
try
{
URL url = new URL(Const.URL_XUESHIJIA_GETNEWVERSION + "?type=zhaji");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
int size = connection.getContentLength();
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[1024];
connection.connect();
Long starttime = System.currentTimeMillis();
while (true)
{
if (inputStream != null)
{
int numRead = inputStream.read(buffer);
if (numRead <= 0)
{
break;
}
else
{
fos.write(buffer, 0, numRead);
Log.i("xjc", "---->" + (int) file.length());
Long stoptime = System.currentTimeMillis();
Long time = (stoptime - starttime) / 1000;
if (time != 0)
{
Message msg = new Message();
int baifenbi = (int) (file.length() * 100 / size);
int speed = (int) ((file.length() / 1024) / time);
int spendTime = (int) (stoptime - starttime) / 1000;
msg.obj = speed;
msg.arg1 = baifenbi;
msg.arg2 = spendTime;
}


}
}
}
connection.disconnect();
fos.close();
inputStream.close();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return file;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值