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

相关推荐
coderlin_7 小时前
BI布局拖拽 (1) 深入react-gird-layout源码
android·javascript·react.js
2501_915918418 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
wen's9 小时前
React Native安卓刘海屏适配终极方案:仅需修改 AndroidManifest.xml!
android·xml·react native
编程乐学10 小时前
网络资源模板--基于Android Studio 实现的聊天App
android·android studio·大作业·移动端开发·安卓移动开发·聊天app
没有了遇见12 小时前
Android 通过 SO 库安全存储敏感数据,解决接口劫持问题
android
hsx66612 小时前
使用一个 RecyclerView 构建复杂多类型布局
android
hsx66612 小时前
利用 onMeasure、onLayout、onDraw 创建自定义 View
android
守城小轩12 小时前
Chromium 136 编译指南 - Android 篇:开发工具安装(三)
android·数据库·redis
whysqwhw13 小时前
OkHttp平台抽象机制分析
android
hsx66614 小时前
Android 内存泄漏避坑
android