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,注意:在销毁的时候

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
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/zhangyp-pipi/p/10690024.html

时间: 2024-08-14 09:35:59

android app内部更新适配到8.0的相关文章

Android App补丁更新

上一周比较忙,忙的不可开交,写的文章也就两篇,在此希望大家见谅.这周呢,突然闲下来了,有时间了,就重构了下代码,捣鼓点前卫的技术,沉淀沉淀.所以呢,今天就分享下这几天研究的东西. 移动互联网主打的就是用户体验和产品的快速迭代,通过用户反馈和用户行为跟踪及时调整产品方向,这样才能持续保持生命力和创造力.说的接地气点就是,你频繁的升级更新,有时只是修复了几个bug或者微调了下界面,就让用户下载10几兆甚至更大的apk,而且在目前国内这个4G还不是普及的时候,对用户来说是很不友好的.有没有这样一种策略

开源 Android App 增量更新库 版本升级

开源 Android App 增量更新库 版本升级 经过几天的重构,我将之前写的一个Android 应用增量更新的示例程序重构为了一个开源库,现在已经push 到 GitHub 上,欢迎大家Watch.Star.Fork. 包含以下内容: 服务器端生成差异包的工程:AppUpdate 客户端使用的开源apk合并库:ApkPatchLibrary 引用ApkPatchLibrary,实现增量更新的ApkPatchLibraryDemo 旧版本的微博Android客户端,以及服务端生成的新旧微博差分

android app 内部文件路径

1 public class MainActivity extends Activity { 2 3 final String FILE_NAME = "crazyit.bin"; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 10

android app崩溃日志收集以及上传

源码获取请到github:https://github.com/DrJia/AndroidLogCollector 已经做成sdk的形式,源码已公开,源码看不懂的请自行google. 如果想定制适应自己app的sdk请自行fork. AndroidLogCollector android app崩溃日志收集sdk 1.0 作者:贾博士 崩溃日志收集方法: 1.LogCollector是lib包,在需要添加崩溃日志sdk的工程中导入此包. 2.导入lib后,在自己的工程的AndroidManife

Android Studio - Gradle 更新升级到2.1.0后,发生Duplicate files copied in APK META-INF

今天升级了Android Studio到2.1正式版,发现其建议更新Gradle版本,于是把Gradle更新到2.1.0,但是更新后项目无法跑,报错:Duplicate files copied in APK META-INF. 之前项目也有遇到同样的问题,只需要在app项目中(我的项目有几个子module)添加以下代码就可以了: packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/LICENSE' exclud

解决android SDK不能更新,appium测试混合app无法返回webview问题

问题:1.connection to the server is unsuccessful(file:///www/asset/index.html)(原因:android系统问题,需要更新)2.appium测试混合app,没有返回webview(原因:主要由于android版本问题,需要原生系统android4.4)3.android SDK不能更新package 4.android SDK中extra文件更新失败问题 解决:一.更新sdk,步骤如下:1.修改hosts文件打开c:/windo

Android实现APP自动更新功能

现在一般的android软件都是需要不断更新的,当你打开某个app的时候,如果有新的版本,它会提示你有新版本需要更新.该小程序实现的就是这个功能. 该小程序的特点是,当有更新时,会弹出一个提示框,点击确定,则在通知来创建一个进度条进行下载,点击取消,则取消更新. 以下是详细代码: 1.创建布局文件notification_item.xml,用于在通知栏生成一个进度条和下载图标. <?xml version="1.0" encoding="utf-8"?>

6盘(清真云)Android APP 测试版 20181011 每日更新

Android APP 20181011 Daily Build 修复: -  用户无法注册 - 注册用户无法登录 - 下载大文件崩溃问题 - 新用户报错 parent directory not found 新增: - 下载时选择文件夹 延后处理问题 - 离线任务删除 原文地址:https://www.cnblogs.com/6pan/p/9772977.html

Retrofit2实现App自动更新

原理 Retrofit2和okhttp实现了apk的下载 自定义类实现Retrofit2的Callback类在里面通过IO流写入文件并且使用RxBus订阅下载进度 自定义类实现okhttp3的ResponseBody类并且在里面使用RxBus发布下载进度信息 在Service中使用Retrofit在后台下载文件 发送Notifaction到通知栏前台界面展示进度情况 实现步骤 1.创建UpdateManger管理类 这个类主要写了两个管理更新和弹框的方法. /** - 检测软件更新 */ pub