首页 > 解决方案 > 在 react-native 项目中清除 android 和 iOS 中的应用程序数据?

问题描述

嗨,我知道以前有人问过类似的问题。提到的解决方案没有帮助。

  1. 试过这个`

      File cache = getReactApplicationContext().getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] test =  cache.list();
            Log.d("before", Arrays.toString(test));
            for (String s : test) {
                //if (s.contains(".pdf") || s.contains(".mp4")) {
                    deleteDir(new File(cache, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
               // }
            }
            Log.d("After delete", Arrays.toString(cache.list()));
        }
    
    public static boolean deleteDir(File dir)
    {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
    

`但什么也没发生

  1. 试过这个`

     try{
     if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
                ((ActivityManager) getReactApplicationContext().getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData(); // note: it has a return value!
            } else {
                String packageName = getCurrentActivity().getPackageName();
                Runtime runtime = Runtime.getRuntime();
                runtime.exec("pm clear " + packageName);
    
    
       } 
      } catch(error){console.log("error"+error)}`
    

在控制台中收到警告 要求循环是允许的,但可能导致未初始化的值。考虑重构以消除对循环的需求。应用程序已退出,这是不鼓励的。我需要在清理数据后显示登录页面。

任何见解???

标签: reactjsreact-nativereact-native-android

解决方案


推荐阅读