app 内部跟新是app中必需要有的功能,在app出现改变时,app内部更新能以最快的速度将应用提高到最新版本。android
步骤:服务器
一、获取本地app的版本号app
int versionCode = 0;
try {
// 获取软件版本号,
versionCode = this.getPackageManager().getPackageInfo(
getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
二、上传服务器检查是否为最新app,对返回的数据进行操做。若是不是最新,则作更新操做。ide
三、调用android手机的DownLoadManager进行apk的下载ui
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,注意:在销毁的时候this
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系统须要申请权限url
@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文件.net
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<external-path
name="download"
path=""/>
</paths>
</resources>
在AndroidManifest.xml设置Providexml
<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
版权声明:本文为博主原创文章,转载请附上博文连接!blog