应用安装

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>

相关推荐

  1. 应用安装

    2024-03-10 22:04:03       35 阅读
  2. Ubuntu下应用软件安装

    2024-03-10 22:04:03       78 阅读
  3. 华为应用市场静默安装

    2024-03-10 22:04:03       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 22:04:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 22:04:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 22:04:03       82 阅读
  4. Python语言-面向对象

    2024-03-10 22:04:03       91 阅读

热门阅读

  1. http cookie,tcp syncookie 和 tcp fastopen 杂谈

    2024-03-10 22:04:03       40 阅读
  2. Ruby CanCanCan 动态定义方法

    2024-03-10 22:04:03       47 阅读
  3. 如何系统的去学c#

    2024-03-10 22:04:03       42 阅读
  4. 【Demo】游戏小地图

    2024-03-10 22:04:03       39 阅读
  5. Pytest教程:Pytest的跳过与标记功能用法

    2024-03-10 22:04:03       39 阅读
  6. C#拾遗补漏之goto跳转语句

    2024-03-10 22:04:03       48 阅读
  7. GRU-深度学习循环神经网络情感分类模型搭建

    2024-03-10 22:04:03       47 阅读
  8. 学习数据结构和算法的地13天

    2024-03-10 22:04:03       39 阅读
  9. 函数柯里化(function currying)及部分求值

    2024-03-10 22:04:03       47 阅读
  10. vue中常用的指令和自定义指令

    2024-03-10 22:04:03       40 阅读
  11. 打印前端代码

    2024-03-10 22:04:03       46 阅读
  12. [leetcode] 283. 移动零

    2024-03-10 22:04:03       45 阅读
  13. 我耀学IT—day06-导航栏

    2024-03-10 22:04:03       34 阅读
  14. 用spark读取及存储数据

    2024-03-10 22:04:03       45 阅读
  15. Gson(List<Object>转String 、String转List<Object>)

    2024-03-10 22:04:03       43 阅读
  16. 手机中常用的传感器

    2024-03-10 22:04:03       41 阅读
  17. vuej介绍

    2024-03-10 22:04:03       49 阅读