2012-06-11 66 views
11

我想要做的是在應用程序退出時清除應用程序的緩存。在Android中退出時清除應用程序緩存

這個任務我可以通過這個步驟手動完成。

<應用程序 - >管理應用程序 - >「我的應用」 - >清除緩存>>

,但我希望通過編程的應用程序的退出做這個任務..請幫助我的傢伙..

在此先感謝..

+1

[如何清除緩存的Android(http://stackoverflow.com/questions/6898090/how-to-clear-cache-android) – Gofurs

回答

15

嘗試這一個 -

import java.io.File; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 

public class HelloWorld extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle *) { 
     super.onCreate(*); 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onStop(){ 
     super.onStop(); 
    } 

    //Fires after the OnStop() state 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
     trimCache(this); 
     } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
    } 

    public static void trimCache(Context context) { 
     try { 
     File dir = context.getCacheDir(); 
     if (dir != null && dir.isDirectory()) { 
      deleteDir(dir); 
     } 
     } catch (Exception e) { 
     // TODO: handle exception 
     } 
    } 

    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; 
      } 
     } 
     } 

     // The directory is now empty so delete it 
     return dir.delete(); 
    } 

} 

請參考以下鏈接 -

+0

您發佈的鏈接並不指向任何有效的內容。 –

+1

不要刪除主線程上的文件!您必須在其他線程上執行這些調用以避免ANR! – Jona

+0

嗨,它也清理數據庫..我們可以跳過嗎? –

9

要清除應用程序數據請嘗試這種方法。我認爲它可以幫助你。

public void clearApplicationData() 
{ 
    File cache = getCacheDir(); 
    File appDir = new File(cache.getParent()); 
    if (appDir.exists()) { 
     String[] children = appDir.list(); 
     for (String s : children) { 
      if (!s.equals("lib")) { 
       deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************"); 
      } 
     } 
    } 
} 

public static boolean deleteDir(File dir) 
{ 
    if (dir != null &amp;&amp; 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(); 
} 
+0

感謝這樣一個美好的答案可能重複。但我擔心的是,當我應該調用此方法時,因爲我在調用此方法的時間方面存在很多混淆 –

+2

在您的應用程序中將出現用戶退出(通常是主要活動)的活動,重寫OnDestroy( )並調用上面的清除緩存代碼。 – Sathesh

+0

嗨,它也是清除數據庫..我們可以跳過嗎? –

2

剛剛澄清,答案正常工作,除非你已經通過了申請上下文到trimCache而不是活動上下文(以避免內存泄漏),因爲trimCache是一種靜態方法。

@Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
     trimCache(getApplicationContext()); //if trimCache is static 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
} 

然而,否則,你可以讓trimCache非靜態的,也沒有必要通過任何語境。

public void trimCache() { 
    try { 
    File dir = getCacheDir(); 
    if (dir != null && dir.isDirectory()) { 
     deleteDir(dir); 
    } 
    } catch (Exception e) { 
    // TODO: handle exception 
    } 
} 
+0

嗨,它也是清除數據庫..我們可以跳過嗎? –

相關問題