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下载逻辑没有实现,需要资格去实现。

相关推荐
恋猫de小郭13 分钟前
Google I/O Extended :2025 Flutter 的现状与未来
android·前端·flutter
@Ryan Ding29 分钟前
MySQL主从复制与读写分离概述
android·mysql·adb
移动开发者1号1 小时前
Android 同步屏障(SyncBarrier)深度解析与应用实战
android·kotlin
移动开发者1号1 小时前
深入协程调试:协程调试工具与实战
android·kotlin
雨白10 小时前
Jetpack系列(三):Room数据库——从增删改查到数据库平滑升级
android·android jetpack
花王江不语13 小时前
android studio 配置硬件加速 haxm
android·ide·android studio
江太翁15 小时前
mediapipe流水线分析 三
android·mediapipe
与火星的孩子对话15 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
tmacfrank16 小时前
Android 网络全栈攻略(四)—— TCPIP 协议族与 HTTPS 协议
android·网络·https
fundroid17 小时前
Kotlin 协程:Channel 与 Flow 深度对比及 Channel 使用指南
android·kotlin·协程