Android 版本升级自动安装
闲来无事写一个小小Demo,本人小白一个大佬们绕行哦
版本升级步骤简单记录一下(大佬跳过):
- 根据版本号判断是否升级(文末提供获取版本号的工具类,当然啦网上一大堆)
- 如果升级下载安装包(文末提供下载的工具类(okhttp))
- 现在完成调用系统安装
- 实时显示下载进度(看需求啦不想就删除。删除。删除)
注意事项
- 权限忘啦就GG啦
- Android7.0的一些麻烦事
- 3.在res文件夹下新建一个xml文件文件
<--适配7.0-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example*******.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
下载代码(记不清是结合哪位大佬的代码啦,看见啦别介意哈)
参数:url:你的apk下载路径 saveDir:你的下载地址 DownloadListener :下载的回调监听文末给出
private void startLoadApk(final String url, final String saveDir, final DownloadListener downloadListener) {
final Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 下载失败
downloadListener.onDownloadFailed();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
downloadListener.onDownloadFailed();
return;
}
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
// 储存下载文件的目录
String savePath = saveDir;
try {
is = response.body().byteStream();
long total = response.body().contentLength();
File file = new File(savePath, getNameFromUrl(url));
fos = new FileOutputStream(file);
long sum = 0;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
// 下载中
downloadListener.onDownloading(progress);
}
fos.flush();
// 下载完成
downloadListener.onDownloadSuccess();
} catch (Exception e) {
downloadListener.onDownloadFailed();
} finally {
try {
if (is != null)
is.close();
} catch (IOException e) {
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
}
}
}
});
}
/**
* @param url
* @return 从下载链接中解析出文件名
*/
private String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
安装
参数:paht:你的安装路径 apkName:下载本地apk的名称(写错啦就安装失败)
File file = new File(path + "/apkName");
private void installApk(File file) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
apkUri = FileProvider.getUriForFile(getActivity()
, "com.example.*******.fileprovider(与功能清单文件中对应)"
, file);
} else {
apkUri = Uri.fromFile(file);
}
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
// 查询所有符合 intent 跳转目标应用类型的应用,注意此方法必须放置在 setDataAndType 方法之后
List<ResolveInfo> resolveLists = getActivity().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
// 然后全部授权
for (ResolveInfo resolveInfo : resolveLists) {
String packageName = resolveInfo.activityInfo.packageName;
getActivity().grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
getActivity().startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
获取版本号
public class GetPackageInfo {
public static int packageCode(Context context) {
PackageManager manager = context.getPackageManager();
int code = 0;
try {
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
code = info.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return code;
}
public static String packageName(Context context) {
PackageManager manager = context.getPackageManager();
String name = null;
try {
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
name = info.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return name;
}
下载的回调监听
public interface DownloadListener {
/**
* 下载成功
*/
void onDownloadSuccess();
/**
* @param progress
* 下载进度
*/
void onDownloading(int progress);
/**
* 下载失败
*/
void onDownloadFailed();
}