簡體   English   中英

如何清除Android版本7.1中的緩存

[英]How to clear cache in android version 7.1

請幫助清除android中的緩存。 我可以在Android 6.0版上使用以下代碼刪除緩存。 當我在7.1中嘗試時,沒有任何變化。 我想念什么?

 //method to delete cache- issue reported
    public static void deleteCache(Context context) {
        try {
            File dir = context.getCacheDir();
            deleteDir(dir);

            Log.d("cache clear", "cache delete "+dir);
        } catch (Exception e) { e.printStackTrace();}
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            Log.d("cache clear", "cache delete 1");


            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();
        } else if(dir!= null && dir.isFile()) {

            return dir.delete();
        } else {
            return false;
        }
    }

在Android API 19中,您可以執行以下操作:

((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();

官方文檔:API級別19中添加了clearApplicationUserData

公共布爾clearApplicationUserData()

允許應用程序從磁盤擦除其自身的數據。 這等效於用戶選擇從設備設置UI中清除應用程序的數據。 它會刪除與該應用程序關聯的所有動態數據-私有數據和外部存儲中私有區域中的數據-但不會刪除已安裝的應用程序本身,也不會刪除任何OBB文件。 它還會撤消該應用程序已獲取的所有運行時權限,清除所有通知並刪除與此應用程序相關的所有Uri授予。

Returns
boolean     true if the application successfully requested that the application's data be erased; false otherwise. 

編輯從您的活動中不打電話

deleteCache(this) 

但是打電話

deleteCache(getApplicationContext()) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM