本篇讲解一下如何在Android各个版本上实现应用内安装APK。需要注意,要适配Android 7.0和Android 8.0。
1.初始化工具类InstallManager(自定义一个类继承于BaseManager)
private static final String TAG = "InstallManager";
private Activity mActivity;
private String mPath;//下载下来后文件的路径
public static final int UNKNOWN_CODE = 2018;
private static InstallManager installManager;
private InstallManager(Activity mActivity, String mPath) {
attachActivity(mActivity);
this.mActivity = obtainActivity();
this.mPath = mPath;
}
public static InstallManager getInstance(Activity mActivity, String mPath) {
if (installManager == null) {
synchronized (InstallManager.class) {
if (installManager == null) {
installManager = new InstallManager(mActivity, mPath);
}
}
}
return installManager;
}
2.Android1.x-6.x版本
/**
* android1.x-6.x
*/
private void startInstall() {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.parse("file://" + mPath), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mActivity.startActivity(install);
detachActivity();
}
3.Android7.x版本的适配
(1)启动函数
/** * android7.x */ private void startInstallN() { //参数1 上下文, 参数2 在AndroidManifest中的android:authorities值, 参数3 共享的文件 Uri apkUri = FileProvider.getUriForFile(mActivity, mActivity.getPackageName() + ".provider", new File(mPath)); Intent install = new Intent(Intent.ACTION_VIEW); //由于没有在Activity环境下启动Activity,设置下面的标签 install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //添加这一句表示对目标应用临时授权该Uri所代表的文件 install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); install.setDataAndType(apkUri, "application/vnd.android.package-archive"); mActivity.startActivity(install); detachActivity(); }
(2)适配Android 7.0
请注意:适配Android 7.0需要在清单文件的application标签下添加 <!-- 适配android7.0及其以上(新版AndroidX) --> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
指定共享的目录上面配置文件中 android:resource="@xml/file_paths" 指的是当前组件引用 res/xml/file_paths.xml 这个文件。
我们需要在资源(res)目录下创建一个xml目录,然后创建一个名为“file_paths”(名字可以随便起,只要和在manifest注册的provider所引用的resource保持一致即可)的资源文件。
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path
name="camera_photos"
path="" />
<!--
files-path: 该方式提供在应用的内部存储区的文件/子目录的文件。
它对应Context.getFilesDir返回的路径:eg:”/data/data/com.***.***/files”。
cache-path: 该方式提供在应用的内部存储区的缓存子目录的文件。
它对应Context.getCacheDir返回的路:eg:“/data/data/com.***.***/cache”;
external-path: 该方式提供在外部存储区域根目录下的文件。
它对应Environment.getExternalStorageDirectory返回的路径
external-files-path: Context.getExternalFilesDir(null)
external-cache-path: Context.getExternalCacheDir(String)
-->
<external-path
name="download"
path="" />
</paths>
</resources>
4.Android8.x版本的适配
(1)启动函数
/**
* android8.x
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallO() {
boolean isGranted = mActivity.getPackageManager().canRequestPackageInstalls();
if (isGranted) {
startInstallN();//安装应用的逻辑(写自己的就可以)
} else {
new AlertDialog.Builder(mActivity)
.setCancelable(false)
.setTitle(mActivity.getResources().getString(R.string.you_need_to_open_the_unknown_source_permission_to_install_the_app__please_open_the_permission_in_settings))
.setPositiveButton(mActivity.getResources().getString(R.string.determine), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int w) {
Uri packageURI = Uri.parse("package:" + mActivity.getPackageName());
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
mActivity.startActivityForResult(intent, UNKNOWN_CODE);
detachActivity();
}
})
.setCancelable(false)
.show();
}
}
(2)适配Android 8
Android8.0的诸多新特性中有一个非常重要的特性:未知来源应用权限
在清单文件manifest标签下添加
<!-- 安装app权限 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
//解释说明
boolean isGranted = getPackageManager().canRequestPackageInstalls();
在代码里面对权限进行处理
如果isGranted为 true,则说明你的应用有安装未知来源应用的权限,你直接执行安装应用的操作即可。
如果isGranted为 false,则说明你的应用没有安装未知来源应用的权限,则无法安装应用。由于这个权限不是运行时权限,所以无法再代码中请求权限,还是需要用户跳转到设置界面中自己去打开权限。
a. 弹出dialog,告知用户 "安装应用需要打开未知来源权限,请去设置中开启权限"
b. 然后用户点击确定之后跳转到未知来源应用权限管理列表:
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
startActivityForResult(intent, UNKNOWN_CODE);
c. 在onActivityResult中去接收结果:
if (resultCode == RESULT_OK && requestCode == InstallUtil.UNKNOWN_CODE) {
startInstallO();//再次执行安装流程,包含权限判等
}
6.重写子类中detachActivity函数
@Override
public void detachActivity() {
super.detachActivity();
if (installManager != null) {
installManager = null;
}
}
5.子工具类
public abstract class BaseManager<T> {
private WeakReference<T> modelActivity;
protected void attachActivity(T modelActivity) {
this.modelActivity = new WeakReference<T>(modelActivity);
}
public void detachActivity() {
if (isAttach()) {
modelActivity.clear();
modelActivity = null;
}
}
public T obtainActivity() {
return isAttach() ? modelActivity.get() : null;
}
protected boolean isAttach() {
return modelActivity != null &&
modelActivity.get() != null;
}
protected boolean isEmpty(String dataStr) {
if (dataStr != null && !"".equals(dataStr)) {
return false;
} else {
return true;
}
}
}
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。
项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2