卧疾丰暇豫,翰墨时间作。这篇文章主要讲述android app内部更新适配到8.0相关的知识,希望能为你提供帮助。
app 内部跟新是app中必须要有的功能,在app出现改变时,app内部更新能以最快的速度将应用提升到最新版本。
步骤:
1、获取本地app的版本号
int versionCode = 0;
try {
// 获取软件版本号,
versionCode = this.getPackageManager().getPackageInfo(
getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
2、上传服务器检查是否为最新app,对返回的数据进行操作。如果不是最新,则做更新操作。
3、调用android手机的DownLoadManager进行apk的下载
request = new DownloadManager.Request(Uri.parse(url));
// 设置通知栏标题
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("一儒快递");
//
request.setDescription("一儒快递开始下载");
request.setAllowedOverRoaming(false);
request.allowScanningByMediaScanner();
// 可被媒体扫描到
request.setVisibleInDownloadsUi(true);
// 可见和管理
File dir = getApplication().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
filePath = dir.toString() + "/" + "insurework" + getVersionCode() + ".apk";
// 设置文件存放目录
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "insurework" + getVersionCode() + ".apk");
did = downManager.enqueue(request);
需要获取下载的状态,所以需要在初始化的时候注册广播监听系统下载完成ACTION,注意:在销毁的时候
【android app内部更新适配到8.0】IntentFilter filter = new IntentFilter();
filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
receiver = new DownLoadCompleteReceiver();
registerReceiver(receiver, filter);
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
自定义的广播接收者,在接收者中作版本适配
private class DownLoadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null &
&
intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
long downid = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (did == downid) {// 如果下载完毕,
try {
if (Build.VERSION.SDK_INT >
= Build.VERSION_CODES.N) {
//判读版本是否在7.0以上(7.0及之后的自动安装需要ContentPrivide来获取到下载的文件)
installApk();
} else if (VERSION.SDK_INT >
= VERSION_CODES.O) {
boolean b = getPackageManager().canRequestPackageInstalls();
if (b) {
installApk();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, Constent.NEW_8_0_REQUEST);
}
} else {
//7.0以前的启动方法
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(downManager.getUriForDownloadedFile(downid), "application/vnd.android.package-archive");
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(install);
}
} catch (Exception e) {
System.out.println(e.getMessage().toString().trim());
}
}
} else if (intent.getAction().equals(
DownloadManager.ACTION_NOTIFICATION_CLICKED)) {
// 点击事件
System.out.println("ACTION_NOTIFICATION_CLICKED");
}
}
}
其中8.0系统需要申请权限
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Constent.NEW_8_0_REQUEST) {
installApk();
}
}
7.0以上自动安装逻辑
private void installApk() {
Uri apkUri = FileProvider.getUriForFile(context, "你的包名.fileprovider", new File(filePath));
//在AndroidManifest中的android:authorities值
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(install);
}
内容提供者:res下新建xml文件夹,新建file_paths.xml文件
<
?xml version="1.0" encoding="utf-8"?>
<
resources>
<
paths>
<
external-path
name="download"
path=""/>
<
/paths>
<
/resources>
在AndroidManifest.xml设置Provide
<
provider
android:name="android.support.v4.content.FileProvider"
android:authorities="你的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<
meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
<
/provider>
结束。
---------------------
作者:han_gao
来源:CSDN
原文:https://blog.csdn.net/yikunhan/article/details/80527884
版权声明:本文为博主原创文章,转载请附上博文链接!
推荐阅读
- Android版本28使用http请求
- Android CollapsingToolbarLayout使用介绍
- Rejecting mapping update to [xxx] as the final mapping would have more than 1 type: [xxx, xx]
- Jquery中 .empty和.append
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错
- R中的文本挖掘(《PokémonGO》提及是否真的在推高股价())
- Python中的推荐系统(入门教程)
- Python字符串用法教程
- R教程(R中的公式)