1.ProviderApkActivity
package com.tiger.chapter07_client;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import com.tiger.chapter07_client.utils.PermissionUtil;
import com.tiger.chapter07_client.utils.ToastUtlis;
import java.io.File;
public class ProviderApkActivity extends AppCompatActivity implements View.OnClickListener {
private static final String[] PERMISSIONS = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE
};
private static final int PERMISSION_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provider_apk);
findViewById(R.id.btn_install).setOnClickListener(this);
}
@Override
public void onClick(View v) {
// Android 11 之后获取 MANAGE_EXTERNAL_STORAGE 权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d("ning", "Android 11+");
checkAndInstall();
} else {
// 如果有权限,直接安装,没有权限则获取权限
if (PermissionUtil.checkPermission(this, PERMISSIONS, PERMISSION_REQUEST_CODE)) {
installApk();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE &&
PermissionUtil.checkGrant(grantResults)) {
installApk();
}
}
@RequiresApi(api = Build.VERSION_CODES.R)
private void checkAndInstall() {
// 检查是否拥有MANAGE_EXTERNAL_STORAGE 权限,没有则跳转到设置页面
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(intent);
} else {
installApk();
}
}
private void installApk() {
String apkPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/chapter06-release.apk";
Log.d("ning", "apkPath:" + apkPath);
// 获取应用包管理器
PackageManager pm = getPackageManager();
// 获取apk文件的包信息
PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
if (pi == null) {
ToastUtlis.show(this, "安装文件已经损坏!");
return;
}
// installer
Uri uri = Uri.parse(apkPath);
// 兼容Android7.0,把访问文件的Uri方式改为FileProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 通过FileProvider获得文件的Uri访问方式
uri = FileProvider.getUriForFile(this, getString(R.string.file_provider), new File(apkPath));
Log.d("ning", String.format("new uri:%s", uri.toString()));
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 设置Uri的数据类型为APK文件
intent.setDataAndType(uri, "application/vnd.android.package-archive");
// 启动系统自带的应用安装程序
startActivity(intent);
}
}
2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ProviderApkActivity">
<Button
android:id="@+id/btn_install"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="install apk" />
</LinearLayout>
3.清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 需要这个防止手机没有打电话功能 -->
<uses-feature
android:name="android.hardware.telephony"
android:required="false" /> <!-- 处于安全考虑 Android 11 要求应用事先说明需要访问的其他软件包 -->
<queries>
<!-- <package android:name="com.tiger.chapter07_server"/> -->
<provider android:authorities="com.tiger.chapter07_server.provider.UserInfoProvider" />
</queries>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" /> <!-- 在api 33版本及以上,READ_EXTERNAL_STORAGE失效了,要使用READ_MEDIA_IMAGES -->
<!-- android 11 需要 安装apk 需要权限-->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<!-- 安装应用请求 Andorid 8.0-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<!-- 读取外部存储权限 -->
<!-- 注意:安卓10以上无法显示图像,还需要在清单应用下面增加android:requestLegacyExternalStorage="true" -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".ProviderApkActivity"
android:exported="false" />
<activity
android:name=".ProviderMmsActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- 用系统的 provider 为第三方应用提供接口获取图片 -->
<!-- android:grantUriPermissions="true" -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="@string/file_provider"
android:grantUriPermissions="true">
<!-- 配置哪些路径是可以通过FileProvider访问的 -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>