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;
    }
相关推荐
车轮滚滚__36 分钟前
uniapp对接unipush 1.0 ios/android
笔记
云边有个稻草人3 小时前
【优选算法】—复写零(双指针算法)
笔记·算法·双指针算法
拭心11 小时前
Google 提供的 Android 端上大模型组件:MediaPipe LLM 介绍
android
冷眼看人间恩怨12 小时前
【Qt笔记】QDockWidget控件详解
c++·笔记·qt·qdockwidget
带电的小王14 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
梦想平凡14 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
元争栈道14 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库15 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
元争栈道16 小时前
webview+H5来实现的android短视频(短剧)音视频播放依赖控件资源
android·音视频
MuYe16 小时前
Android Hook - 动态加载so库
android