Android 版本升级(APK下载和安装)

将手机上应用版本号等相关信息发送到服务器,服务器检测是否有新版本。如果有新版本,将返回客户端版本号、版本名称、下载地址、版本更新说明等信息。客户端获取到此信息后,将询问用户是否下载,确定后,开始下载。下载完成后开始提示安装。


调用系统DownloadManager实现下载:

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request( 版本下载地址);

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "xxx.apk");//将apk下载到download默认目录
request.allowScanningByMediaScanner();
request.setMimeType("application/cn.trinea.download.file");
long downloadId = downloadManager.enqueue(request);//每个下载请求对应一个downloadId

PrefUtils.setLong(PrefUtils.DOWNLOAD_ID, downloadId);//暂存到Preference中

下载完成后,将发送DownloadManager.ACTION_DOWNLOAD_COMPLETE广播。因此需要建立一个Broadcast Receiver来接收广播,确认下载成功后进行安装提示。


public class DownloadCompleteReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

		if (completeDownloadId != -1 && completeDownloadId == PrefUtils.getLong(PrefUtils.DOWNLOAD_ID, -1)) {

			DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
			DownloadManager.Query query = new DownloadManager.Query().setFilterById(completeDownloadId);
			Cursor cursor = null;
			String filename = null;
			try {
				cursor = downloadManager.query(query);
				if (cursor != null && cursor.moveToFirst()) {
					
					filename = cursor.getString(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_FILENAME));
				}
			} finally {
				if (cursor != null) {
					cursor.close();
				}
			}

			if (!TextUtils.isEmpty(filename)) {

				Intent installIntent = new Intent(Intent.ACTION_VIEW);
				installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				installIntent.setDataAndType(Uri.fromFile(new File(filename)),"application/vnd.android.package-archive");
				context.startActivity(installIntent);
			}
		}

	}

}


Android应用开发中,下载安装APK升级应用版本通常涉及几个步骤:下载APK文件、安装APK文件以及确保新版本与Android 30的兼容性。以下是一个简化的过程和示例代码来说明如何实现这样的升级机制。 1. **下载APK文件**:首先,你需要从服务器下载新版本的APK文件。这通常通过网络请求来实现,比如使用OkHttp库下载文件。 ```java // 示例代码,用于从网络下载APK文件 OkHttpClient client = new OkHttpClient(); String url = "http://yourserver.com/yourapp.apk"; // 替换为实际的APK下载链接 Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 处理下载失败的情况 } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { // 下载成功,保存APK文件到应用的私有目录 File apkFile = new File(getFilesDir(), "update.apk"); try (FileOutputStream fos = new FileOutputStream(apkFile); InputStream is = response.body().byteStream()) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } } // 下载完成后,调用安装APK的方法 installApk(apkFile); } } }); ``` 2. **安装APK文件**:一旦APK文件下载并保存在设备上,你可以使用`Intent`和`Uri`来启动安装过程。注意,从Android N(7.0)开始,需要适配“FileProvider”来分享文件。 ```java // 示例代码,用于安装APK文件 private void installApk(File apkFile) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri apkUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", apkFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); startActivity(intent); } else { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } ``` 3. **适配Android 30**:为了确保新版本应用与Android 30的兼容性,你需要遵循新的权限模型、存储访问框架(Scoped Storage)以及隐私相关的更新。例如,更新你的应用以使用分区存储。 ```xml <!-- 在AndroidManifest.xml中添加属性以启用分区存储 --> <manifest ...> <application android:requestLegacyExternalStorage="true"> ... </application> </manifest> ``` 请注意,`requestLegacyExternalStorage`是一个过渡属性,用于在Android 10上请求完整的外部存储访问权限。对于Android 11及以上版本,你可能需要使用分区存储或者动态请求权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值