在android上边用代码安装apk文件。
1. 首先是下载apk文件到手机app目录下边。
/data/data/com.example.webviewdemo/files/sina.apk
2. 有了这个apk再用代码才能进行下载。
3. E:\andoird-demo\webviewDemo\app\src\main\AndroidManifest.xml 里边添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
4. 添加
<provider
android:authorities="com.example.webviewdemo.fileProvider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
完整的xml如下:
android:authorities="com.example.webviewdemo.fileProvider" 这个就是后边文章里的部分重要。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="com.example.webviewdemo.fileProvider"
android:name="androidx.core.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
</manifest>
5. 同时在: res\xml\file_provider_paths.xml 新建这个文件
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="external" path="." /> <external-files-path name="external_files" path="." /> <cache-path name="cache" path="." /> <external-cache-path name="external_cache" path="." /> <files-path name="files" path="." /> </paths>
这个文件是用来找apk文件的,调用安装函数时会崩溃。 里边有cache,files目录,我测试文件就下载在这里了(/data/data/com.example.webviewdemo/files/sina.apk)
下载的函数:
public boolean installApk(String url){
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//String pathApk = Environment.getDataDirectory()+"/data"+"/com.example.webviewdemo/files/abc.apk";
String pathApk = downLoadDbMgr.createPath(url);
File apkFile = new File( pathApk );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), "com.example.webviewdemo.fileProvider", apkFile);
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
install.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
install.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
startActivity(install);
return true;
}
后边是引别人的文章:
private void install(String filePath) {
Log.i(TAG, "开始执行安装: " + filePath);
File apkFile = new File(filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.w(TAG, "版本大于 N ,开始使用 fileProvider 进行安装");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(
mContext
, "你的包名.fileprovider"
, apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
Log.w(TAG, "正常进行安装");
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
startActivity(intent);
}
代码说明
关于在代码中安装 APK 文件,在 Android N 以后,为了安卓系统为了安全考虑,不能直接访问软件,需要使用 fileprovider 机制来访问、打开 APK 文件。
上面的 if 语句,就是区分软件运行平台,来对 intent 设置不同的属性。
适配 Android 各个版本,使用代码安装 APK
第一步:
在清单文件(manifests.xml)application 标签中增加 <provider> 标签:
<application>
<!--其他的配置项-->
<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>
<!--其他的配置项-->
</application>
注意两点内容:
1. android:authorities="你的包名.fileprovider" 这个属性要设置成你自己的包名。
2. <meta-data> 标签下 android:resource="@xml/file_paths" 是要配置的 xml 文件,他的内容如下:
第二步:
在 res/xml 下增加文件: file_paths.xml 该文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="your_name"
path="your_path" />
</paths>
上面的两个属性要根据自己的使用来配置。
其中 <external-path> 就是手机的外置存储目录。
第三步:
在 Java 代码中使用最上面的代码,问题解决。
这里面有个要注意的点:
清单文件中的 android:authorities="你的包名.fileprovider" 和 JAVA 代码中:
Uri contentUri = FileProvider.getUriForFile(
mContext
, "你的包名.fileprovider"
, apkFile);
中绿色背景的字段必须一致,否则会报错。