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

相关推荐
用户20187928316712 分钟前
Java 线程池:工厂流水线的高效工人管理
android
用户20187928316730 分钟前
Android 耗电统计:城市能源管理的 "电力侦探" 故事
android
ajassi200039 分钟前
开源 java android app 开发(十二)封库.aar
android·java·linux·开源
恋猫de小郭1 小时前
Flutter 小技巧之:实现 iOS 26 的 “液态玻璃”
android·前端·flutter
帅次1 小时前
Flutter Container 组件详解
android·flutter·ios·小程序·kotlin·iphone·xcode
ThMoonAdSixPence1 小时前
在Android Studio中复现AOSP原生GL2JNI图形渲染应用
android·android studio
用户2018792831671 小时前
Android 系统稳定性侦探事务所:超时与崩溃案件调查
android
用户2018792831671 小时前
Android 侦探事务所:Throwable 侦探的案件追踪手册
android
用户2018792831671 小时前
Android 日志邮局:logd 的信件处理全流程
android
用户2018792831671 小时前
Android 城市事件记录局:DropBoxManagerService 的工作日常
android