Android 11 访问 Android/data/或者getExternalCacheDir() root方式

前言

需求要求安装三方应用ExternalCacheDir()下载下来的apk文件。

getExternalCacheDir() : /storage/emulated/0/Android/data/com../cache/

获取访问权限

如果手机安卓版本为Android10的时候,可以在AndroidManifest.xml中添加下列代码

复制代码
   android:requestLegacyExternalStorage="true"

以此禁用分区存储,但这在Android11及以上版本不起作用。

root方式非root方式点这里

  • 第一种:通过adb命令修改

    复制代码
      adb shell
      su
      chmod -R 775 /storage/emulated/0/Android/data/packageName/
  • 第二种:系统服务代码

    复制代码
      frameworks/base/services/core/java/com/android/service/***/***.java
    
      //系统服务合适的地方添加安装卸载应用监听
      private void registerForBroadcasts() {
          IntentFilter intentFilter = new IntentFilter();
          intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
          intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
          intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
          intentFilter.addDataScheme("package");
    
          mContext.registerReceiver(
              new BroadcastReceiver() {
                  @Override
                  public void onReceive(Context context, Intent intent) {
                      String action = intent.getAction();
                      if (Intent.ACTION_PACKAGE_ADDED.equals(action)
                          ||Intent.ACTION_PACKAGE_REPLACED.equals(action) ) {
                          Uri data = intent.getData();
                          String pkgName = data.getEncodedSchemeSpecificPart();
                          //添加安装替换应用监听
                          if("com.***.***".equals(pkgName)){
                              //更新系统属性
                              SystemProperties.set("persist.sys.***", "true");
                          }
                      } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
                          if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                              Uri data = intent.getData();
                              String ssp;
                              if (data != null && (ssp = data.getSchemeSpecificPart()) != null) {
                                  String unPkgName = data.getEncodedSchemeSpecificPart();
                                  //添加移除卸载应用监听
                                  if("com.***.***".equals(unPkgName)){
                                      //更新系统属性
                                      SystemProperties.set("persist.sys.***", "false");
                                  }
                              }
                          }
                      }
                  }
              }, intentFilter);
      }
    
    	system/core/rootdir/init.rc
    
      #init.rc添加属性监听chmod改变权限
      on property:persist.sys.*** = true
          chmod 0775 /storage/emulated/0/Android/data/packageName/
          chmod 0775 /storage/emulated/0/Android/data/packageName/cache
          chmod 0775 /storage/emulated/0/Android/data/packageName/cache/*.apk
  • 第三种:系统服务代码

    复制代码
          //在系统安装应用接口中添加
          if(!TextUtils.isEmpty(filePath)){
              if(filePath.startsWith("/storage/emulated/0/Android/data/com.***.***/")){
                  try {
                          String command = "chmod -R 775 /storage/emulated/0/Android/data/com..***.***/";
                          Process process = Runtime.getRuntime().exec(command);
                          process.waitFor();
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
              }
          }
相关推荐
煤球王子8 小时前
学习记录:Android14中的WiFi-wpa_supplicant(1)
android
张小潇9 小时前
AOSP15 Input专题InputDispatcher源码分析
android
TT_Close9 小时前
【Flutter×鸿蒙】debug 包也要签名,这点和 Android 差远了
android·flutter·harmonyos
Kapaseker10 小时前
2026年,我们还该不该学编程?
android·kotlin
雨白1 天前
Android 快捷方式实战指南:静态、动态与固定快捷方式详解
android
hqk1 天前
鸿蒙项目实战:手把手带你实现 WanAndroid 布局与交互
android·前端·harmonyos
LING1 天前
RN容器启动优化实践
android·react native
恋猫de小郭1 天前
Flutter 发布官方 Skills ,Flutter 在 AI 领域再添一助力
android·前端·flutter
Kapaseker1 天前
一杯美式搞懂 Any、Unit、Nothing
android·kotlin
黄林晴1 天前
你的 Android App 还没接 AI?Gemini API 接入全攻略
android