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;
    }
相关推荐
Eastsea.Chen20 分钟前
MTK Android12 user版本MtkLogger
android·framework
冰帝海岸4 小时前
01-spring security认证笔记
java·笔记·spring
小二·5 小时前
java基础面试题笔记(基础篇)
java·笔记·python
长亭外的少年8 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
wusong9998 小时前
mongoDB回顾笔记(一)
数据库·笔记·mongodb
猫爪笔记8 小时前
前端:HTML (学习笔记)【1】
前端·笔记·学习·html
Resurgence038 小时前
【计组笔记】习题
笔记
pq113_69 小时前
ftdi_sio应用学习笔记 3 - GPIO
笔记·学习·ftdi_sio
爱米的前端小笔记10 小时前
前端八股自学笔记分享—页面布局(二)
前端·笔记·学习·面试·求职招聘
建群新人小猿10 小时前
会员等级经验问题
android·开发语言·前端·javascript·php