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

相关推荐
运维Z叔1 小时前
云安全 | AWS S3存储桶安全设计缺陷分析
android·网络·网络协议·tcp/ip·安全·云计算·aws
Reese_Cool3 小时前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
平凡シンプル3 小时前
安卓 uniapp跨端开发
android·uni-app
elina80133 小时前
安卓实现导入Excel文件
android·excel
严文文-Chris3 小时前
【设计模式-享元】
android·java·设计模式
趋势大仙4 小时前
SQLiteDatabase insert or replace数据不生效
android·数据库
DS小龙哥4 小时前
QT For Android开发-打开PPT文件
android·qt·powerpoint
试行5 小时前
Android实现自定义下拉列表绑定数据
android·java
Dingdangr10 小时前
Android中的Intent的作用
android
技术无疆10 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入