Android APK version update download and install, support 7.0 installation
Function:
1: Download APK. 2: Install APK
Copy the code
How to add
1. Add the warehouse address in the build.gradle file of Project
allprojects {
repositories {
..
maven { url "https://jitpack.io"}}}Copy the code
dependencies {
compile 'Com. Making. Maning0303: MNUpdateAPK: V1.0.3'
}Copy the code
*** For details about 7.0 adaptation, see Library (already configured in library):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="The ${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider" />
</provider>Copy the code
2:res/xml/file_provider.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!--upgrade-->
<external-cache-path
name="mn_update_external_cache"
path="" />
<cache-path
name="mn_update_cache"
path="" />
</paths>Copy the code
public static void installAPK(Context context, String filePath) {
try {
Intent intent = new Intent(a); intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File apkFile = new File(filePath);
if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//The authority is the same as the authorities added to the provider in manifest.xml
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
context.startActivity(intent);
} catch (Exception e) {
Log.e("InstallUtils"."APK installation failed:" + e.toString()); }}Copy the code
Usage:
//Download address of the latest APK
public static final String APK_URL = "http://mobile.ac.qq.com/qqcomic_android.apk";
//APK name after download
public static final String APK_NAME = "update";
//download
new InstallUtils(context, APK_URL.APK_NAME.new InstallUtils.DownloadCallBack() {
@Override
public void onStart() {
Log.i(TAG."InstallUtils---onStart");
numberProgressBar.setProgress(0);
}
@Override
public void onComplete(String path) {
Log.i(TAG."InstallUtils---onComplete:" + path);
InstallUtils.installAPK(context, path, new InstallUtils.InstallCallBack() {
@Override
public void onComplete() {
Toast.makeText(context, "Installing program".Toast.LENGTH_SHORT).show();
}
@Override
public void onFail(Exception e) {
Toast.makeText(context, "Installation failure:" + e.toString(), Toast.LENGTH_SHORT).show(); }}); numberProgressBar.setProgress(100);
}
@Override
public void onLoading(long total.long current) {
Log.i(TAG."InstallUtils----onLoading:-----total:" + total + ",current:" + current);
numberProgressBar.setProgress((int) (current * 100 / total));
}
@Override
public void onFail(Exception e) {
Log.i(TAG."InstallUtils---onFail:" + e.getMessage()); }}).downloadAPK();
Copy the code
Default download path:
/Android/data/The package name/cache/
Copy the code