安卓开发:清除缓存并重启,删除指定路径下的文件缓存


直接上代码,仔细看注释

java 复制代码
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.io.BufferedReader;
import java.io.File;
--导入相关依赖,不同环境版本导入不同,根据实际情况import


public static String clearSTars(String input) throws JSONException {
        String path = appActivity.getApplication().getFilesDir().getPath() + "/zhixing";//这里填指定需要清理的内部文件的路径
        File file = new File(path);
        deleteFile(file);
        clearAppCache(AppActivity.getAppContext());
        restartApp(AppActivity.getAppContext());
        return "";
}

 private static void deleteFile(File file) {
 //删除指定路径文件
        if (file == null || !file.exists()) return;

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    deleteFile(f);
                }
            }
        }
        try {
            boolean deleted = file.delete();
            if (!deleted) {
            }
        } catch (Exception e) {
        }
    }

public static void clearAppCache(Context context) {
//清理app缓存
        try {
            File cacheDir = context.getCacheDir();
            deleteDir(cacheDir);

            // 清除 WebView 缓存
            context.deleteDatabase("webview.db");
            context.deleteDatabase("webviewCache.db");

            File appDir = context.getExternalCacheDir();
            if (appDir != null) {
                deleteDir(appDir);
            }

            Log.i("App", "缓存已清理");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


public static void restartApp(Context context) {
//重启app
        Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

        // 杀死当前进程
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

  public static Context getAppContext() {
        return appActivity.getApplicationContext();
    }
相关推荐
StarsGreen1 小时前
Android VINTF 中 Device Manifest 与 Framework Manifest 详解
android
FungLeo2 小时前
FengCMS ·PHP+MySQL升级实录 · 富文本清洗配置实战:白名单、URI scheme、禁 iframe/id、实例缓存
mysql·缓存·php·xss·fengcms
Name_NaN_None3 小时前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
恋猫de小郭3 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
敢敢のwings14 小时前
GR00T 2:当机器人学会先想象未来,再决定怎么动
android·机器人
传奇开心果编程14 小时前
【Jetpack Compose娓娓道来】第55课:传感器与设备姿态——把物理世界的数据接进Compose
android·kotlin·移动开发·传感器·jetpack compose·ui框架·声明式ui
传奇开心果编程15 小时前
【Jetpack Compose娓娓道来】第52课:蓝牙与位置权限——把“附近”和“在哪”安全地接进Compose
android·kotlin·移动开发·蓝牙·jetpack compose·声明式ui·位置权限
alexhilton16 小时前
卡顿定位:从 UI 线程到 RenderThread
android·kotlin·android jetpack
FungLeo17 小时前
FengCMS· PHP+MySQL升级实录 · 后台参数统一治理:id[]数组、id=undefined 跳转与312组回调组合
android·mysql·php·工程实践·fengcms
北斗星_And17 小时前
七年后再看 Android 底层:机制没变,路变短了
android·ai编程