android 版本更新下载,Android下载新版本并更新(DownLoadManager、HttpURLConnection)

1、使用HttpURLConnection下载并更新

1、下载APK文件

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private File download() {

try {

URL url = new URL("要下载apk文件的路径");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setConnectTimeout(5000);

connection.setRequestMethod("GET");

InputStream inputStream = null;

Log.e("------->","connection.getResponseCode()"+connection.getResponseCode());

if (connection.getResponseCode() == 200) {

int fileSize = connection.getContentLength() / 1024;

Log.e("------->","fileSize"+fileSize);

progressDialog.setMax(fileSize);

int totla = 0;

inputStream = connection.getInputStream();

File file = new File(SAVE_PATH);

FileOutputStream fos = new FileOutputStream(file);

byte[] bytes = new byte[1024];

int len = 0;

while ((len = inputStream.read(bytes)) != -1) {

fos.write(bytes, 0, len);

totla += (len / 1024);

progressDialog.setProgress(totla);

}

//别忘了刷新哈

fos.flush();

fos.close();

inputStream.close();

progressDialog.dismiss();

return file;

}

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2、安装apk文件

Intent intent = new Intent();

intent.setAction("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

//下载的apk文件(download方法返回的File文件)

intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");

startActivity(intent);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2、使用DownLoadManager下载并更新

利用这个DownLoadManager方法下载,会以通知的方式显示,所以应用到广播来通知下载完毕。

1、使用DownLoadManager下载文件,并将downLoadID存到本地

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(" 要更新的文件的.apk的路径"));

//.apk的存储路径       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"haha.apk");

//通知栏是否显示      request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setTitle("通知栏上显示的标题");

request.setDescription("通知栏的描述");

request.setMimeType("application/vnd.android.package-archive");

long downLoadId = downloadManager.enqueue(request);

//将downloadid存到本地

SharedPreferences sharedPreferences = getSharedPreferences("download_id.txt",MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putLong("downLoadId",downLoadId);

editor.commit();

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2、定义广播

public class ApkInstallReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

SharedPreferences sharedPreferences =context.getSharedPreferences("download_id.txt",MODE_PRIVATE);

long downloadApkId = sharedPreferences.getLong("downLoadId",0);

if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {

installApk(context, downloadApkId);

}

}

private static void installApk(Context context, long downloadApkId) {

DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

Intent install = new Intent(Intent.ACTION_VIEW);

Uri downloadFileUri = dManager.getUriForDownloadedFile(downloadApkId);

if (downloadFileUri != null) {

install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");

//从广播启动一个Activity要加这句话

install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(install);

} else {

}

}

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

3、在AndroidManifest中注册广播(两种方式)

方式一:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

方式二:

ApkInstallReceiver receiver = new ApkInstallReceiver();

IntentFilter intent = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

registerReceiver(receiver, intent);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

小奋斗文章

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值