V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zinwalin
V2EX  ›  Android

安卓 10 非系统 App 无法安装 apk 文件

  •  
  •   zinwalin · 2022-07-22 02:53:31 +08:00 · 4499 次点击
    这是一个创建于 616 天前的主题,其中的信息可能已经有所发展或是发生改变。

    定制的 Android 10 系统下,有系统源码,在一个非系统 App 里会调用一个自己开发的库,通过这个库去调用里面的安装 apk 的接口 install ,但是一直报下面的错误,网上有些资料是说可以把 PackageInstaller.apk 这个 app 放到系统里,这样可以绕过安卓 10 的限制,如果此 app 是系统级 app ,那肯定可以安装,之前有做过。

    想请教有没有别的办法可以让非系统 app 安装 apk ,有示例最好了,感谢大佬们。

    库接口:

    public static boolean install(Context context, String filePath) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        File file = new File(filePath);
        if (!file.exists() || !file.isFile() || file.length() <= 0) {
            return false;
        }
    
        i.setDataAndType(Uri.parse("file://" + filePath),
                "application/vnd.android.package-archive");
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
        return true;
    }
    

    错误:

     Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///sdcard/gime.apk typ=application/vnd.android.package-archive flg=0x10000000 }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2058)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1716)
        at android.app.ContextImpl.startActivity(ContextImpl.java:957)
        at android.app.ContextImpl.startActivity(ContextImpl.java:928)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:383)
        at com.eight.touchoneframework.util.PackageUtils.install(PackageUtils.java:76)
    
    8 条回复    2022-07-27 10:36:24 +08:00
    q503315508
        1
    q503315508  
       2022-07-22 08:55:49 +08:00
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    q503315508
        2
    q503315508  
       2022-07-22 09:10:49 +08:00
    还得使用 Fileprovider
    CFM880
        3
    CFM880  
       2022-07-22 10:53:32 +08:00
    ```
    public static void installApk(Context mContext, File file) {
    try {
    DebugLogger.d(TAG, ">>>>>>>>>>>>>>>>>>.. install");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    // 由于没有在 Activity 环境下启动 Activity,设置下面的标签
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在 7.0 以上
    //参数 1 上下文, 参数 2 Provider 主机地址 和配置文件中保持一致 参数 3 共享的文件
    Uri apkUri =
    FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID+ ".fileProvider", file);
    //添加这一句表示对目标应用临时授权该 Uri 所代表的文件
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);;
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    } else {
    intent.setDataAndType(Uri.fromFile(file),
    "application/vnd.android.package-archive");
    }
    DebugLogger.d(TAG, ">>>>>>>>>>>>>>>>>>.. finish");
    mContext.startActivity(intent);
    } catch (Exception err) {
    Toast.makeText(App.getInstance(), Log.getStackTraceString(err), Toast.LENGTH_SHORT).show();
    }
    }
    ```
    想要静默安装的话,还是得参考这个,写一个单独的 apk ,放到系统里

    https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/content/InstallApkSessionApi.java
    zinwalin
        4
    zinwalin  
    OP
       2022-07-24 01:30:31 +08:00
    @CFM880
    谢谢回复, 按照你的代码,运行后出现下面的异常,com.androidnetworking 是我添加的一些网络库。

    java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.androidnetworking.fileProvider
    zinwalin
        5
    zinwalin  
    OP
       2022-07-24 01:30:45 +08:00
    @q503315508 还是一样的问题。
    q503315508
        6
    q503315508  
       2022-07-25 08:26:44 +08:00
    这只是一个片段,还需要去 xml 中声明 provider 。另外你可以试着你 google 下,我搜索了下 ,第一个就是正确的解决方案。
    cczhrd
        7
    cczhrd  
       2022-07-25 15:59:04 +08:00
    http://lazybios.com/2016/12/install-apk-by-intent-compatible-adnroid-n/ 参考这个 再加上这几个权限
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    zinwalin
        8
    zinwalin  
    OP
       2022-07-27 10:36:24 +08:00
    @cczhrd 多谢分享
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5204 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:23 · PVG 09:23 · LAX 18:23 · JFK 21:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.