Android笔记--应用安装

这一节了解一下普通应用安装app的方式,主要是唤起系统来安装,直接上代码:

申请权限

XML 复制代码
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="com.example.permission.SOME_PERMISSION" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES"
        tools:ignore="ProtectedPermissions" />
    <uses-permission
        android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />
    <uses-permission
        android:name="android.permission.ACCESS_SUPERUSER" />
    <uses-permission
        android:name=".PERMISSION" />

    <uses-permission
        android:name="android.permission.GET_PACKAGE_SIZE" />
    <uses-permission
        android:name="android.permission.MANAGE_DOCUMENTS"
        tools:ignore="ProtectedPermissions" />

 <application>
...
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.dc.cplustest.fileProvider"
            android:exported="false"
            android:permission="com.example.permission.SOME_PERMISSION"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
        </provider>
...
</application>

res/xml/filepaths.xml

Activity中:

java 复制代码
private static final String[] PERMISSIONS = new String[]{
        android.Manifest.permission.ACCESS_COARSE_LOCATION,
        android.Manifest.permission.READ_EXTERNAL_STORAGE,
        android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.MANAGE_EXTERNAL_STORAGE,
        android.Manifest.permission.INSTALL_PACKAGES,
        Manifest.permission.WRITE_SETTINGS,
        Manifest.permission.REQUEST_INSTALL_PACKAGES
};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        checkInstallPermission();
        initView();
    }


 public String apk = Environment.getExternalStorageDirectory() + File.separator + "Test" + File.separator + "APK" + File.separator+"fenshen.apk";
    private void initView() {

        if (!checkPermissions(PERMISSIONS)) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, 1000);
        }
        findViewById(R.id.btn_test_so).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    install();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public void checkInstallPermission() {
        boolean haveInstallPermission;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            haveInstallPermission = getPackageManager().canRequestPackageInstalls();
            Log.d("Test","Test haveInstallPermission:"+haveInstallPermission);
            if (!haveInstallPermission) {//没有权限让调到设置页面进行开启权限;
                Uri packageURI = Uri.parse("package:" + getPackageName());
                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
                startActivityForResult(intent, 10086);
            } 
        } 
    }

    public void install() {
        File apkFile = new File(apk);
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //注意第二个参数,要保持和manifest中android:authorities的值相同
        Uri uri = FileProvider.getUriForFile(SecondActivity.this,
                SecondActivity.this.getPackageName() + ".fileProvider", apkFile);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.setData(uri);
        startActivity(intent);
    }

 private boolean checkPermissions(String[] neededPermissions) {
        if (neededPermissions == null || neededPermissions.length == 0) {
            return true;
        }
        boolean allGranted = true;
        for (String neededPermission : neededPermissions) {
          Log.d("Test","权限"+neededPermission+"是否已给: "+(ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED));
            allGranted &= ContextCompat.checkSelfPermission(this, neededPermission) == PackageManager.PERMISSION_GRANTED;
        }
        return allGranted;
    }
相关推荐
努力变厉害的小超超26 分钟前
ArkTS中的组件基础、状态管理、样式处理、class语法以及界面渲染
笔记·鸿蒙
aloha_7895 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
dsywws6 小时前
Linux学习笔记之vim入门
linux·笔记·学习
A-超8 小时前
vue3展示pag格式动态图
笔记
帅得不敢出门9 小时前
安卓设备adb执行AT指令控制电话卡
android·adb·sim卡·at指令·电话卡
u0101526589 小时前
STM32F103C8T6学习笔记2--LED流水灯与蜂鸣器
笔记·stm32·学习
weixin_518285059 小时前
深度学习笔记10-多分类
人工智能·笔记·深度学习
丘狸尾9 小时前
ubuntu【桌面】 配置NAT模式固定IP
笔记
王俊山IT9 小时前
C++学习笔记----10、模块、头文件及各种主题(二)---- 预处理指令
开发语言·c++·笔记·学习
慕卿扬9 小时前
基于python的机器学习(二)—— 使用Scikit-learn库
笔记·python·学习·机器学习·scikit-learn