Android app安装第三方应用

在Android设备上安装第三方应用通常涉及一系列步骤,这些步骤可能会因Android版本的不同而有所差异。但大致流程可以归纳为以下几个关键步骤:

1、开启权限

开启"允许安装未知来源应用",可以去安全设置那里设置允许安装未知来源,也可以使用代码跳转

2、在AndroidManifest.xml添加了权限

复制代码
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

3、判断是否有权限

复制代码
    if(isUnknownSourcesEnabled){
        //权限没有打开则提示用户去手动打开
        openInstallPermission()
    }


    /**
     * 跳转到设置-允许安装未知来源-页面
     */
    private void openInstallPermission() {
          Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
          startActivity(intent);

    }
    

   /**
     * 判断
     * 是否允许
     * 安装位置来源
     */
    public boolean isUnknownSourcesEnabled(Context context) {
        try {
            return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS) == 1;
        } catch (Settings.SettingNotFoundException e) {
            return false;
        }
    }

4、执行安装

复制代码
filePath为绝对路径,例如demo.apk位于file/d/下,则filePath就为/file/d/demo.apk
复制代码
   /**
     * 安装Apk
     */
    public void installApk(String filePath) {

        File apkFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (apkFile.exists()) {
            Uri apkUri = FileProvider.getUriForFile(mContext(),  mContext().getPackageName() + ".fileprovider", apkFile);

            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {
                startActivity(intent);
            } catch (android.content.ActivityNotFoundException ex) {
                // 安装器未找到,可能用户未启用安装未知来源
                Logger.d("===安装器未找到==" + ex.toString());
            }
        } else {
            Logger.d("===文件不存在===");
        }

    }

5、对了android7以上还需要配置FileProvider

复制代码
    <!-- 读写sd卡文件处理 -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="csu.xiaoya.robotApp.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:ignore="WrongManifestParent">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

6、file_paths文件内容

复制代码
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="root" path="."/>
    <files-path name="files" path="." />
    <cache-path name="cache" path="." />
    <external-path name="external" path="." />
    <external-files-path name="name" path="path" />
    <external-cache-path name="name" path="path" />
    <external-path name="external_files" path="."/>
</paths>

最后提示apk下载逻辑没有实现,需要资格去实现。

相关推荐
用户0273851840268 小时前
[Android]RecycleView的item用法
android
前行的小黑炭9 小时前
Android :为APK注入“脂肪”,论Android垃圾代码在安全加固中的作用
android·kotlin
帅得不敢出门10 小时前
Docker安装Ubuntu搭建Android SDK编译环境
android·ubuntu·docker
tangweiguo0305198710 小时前
Android Kotlin 动态注册 Broadcast 的完整封装方案
android·kotlin
fatiaozhang952710 小时前
浪潮CD1000-移动云电脑-RK3528芯片-2+32G-安卓9-2种开启ADB ROOT刷机教程方法
android·网络·adb·电脑·电视盒子·刷机固件·机顶盒刷机
前行的小黑炭11 小时前
Android 不同构建模式下使用不同类的例子:如何在debug模式和release模式,让其使用不同的类呢?
android·kotlin·gradle
andyguo11 小时前
AI模型测评平台工程化实战十二讲(第一讲:从手工测试到系统化的觉醒)
android
2501_9159214311 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
幂简集成12 小时前
通义灵码 AI 程序员低代码 API 课程实战教程
android·人工智能·深度学习·神经网络·低代码·rxjava
2501_9160088913 小时前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview