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.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import com.tiger.chapter07_client.utils.PermissionUtil;
import com.tiger.chapter07_client.utils.ToastUtlis;
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;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provider_apk);
findViewById(R.id.btn_install).setOnClickListener(this);
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d("ning", "Android 11+");
if (PermissionUtil.checkPermission(this, PERMISSIONS, PERMISSION_REQUEST_CODE)) {
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE &&
PermissionUtil.checkGrant(grantResults)) {
@RequiresApi(api = Build.VERSION_CODES.R)
private void checkAndInstall() {
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));
private void installApk() {
String apkPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/chapter06-release.apk";
Log.d("ning", "apkPath:" + apkPath);
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
ToastUtlis.show(this, "安装文件已经损坏!");
Uri uri = Uri.parse(apkPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
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);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
2.xml
<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">
android:id="@+id/btn_install"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="install apk" />
3.清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
android:name="android.hardware.telephony"
android:required="false" />
<provider android:authorities="com.tiger.chapter07_server.provider.UserInfoProvider" />
<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" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
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">
android:name=".ProviderApkActivity"
android:exported="false" />
android:name=".ProviderMmsActivity"
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
android:name="androidx.core.content.FileProvider"
android:authorities="@string/file_provider"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />