2015-04-22 92 views
0

在android应用程序中,我想创建一个应用程序,该应用程序具有一个名为CleanCache的按钮,并在代码中列出所有应用程序缓存并使用Toast消息清除缓存清除。我看着不同的问题和答案,但没有发现任何有趣的事情。我已经实施,但它包含错误。在android程序清除应用程序的缓存不适用于图像

这里是我的MainActivity.java文件:

package com.example.ahsankhan.cache; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

import java.io.File; 

public class MainActivity extends Activity { 

    @Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    addListenerOnButton(); 

} 

public void addListenerOnButton() { 

    //Select a specific button to bundle it with the action you want 
    Button button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 

      @Override 
      public void onCreate(Bundle *) { 
       super.onCreate(*); 
       setContentView(R.layout.activity_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(); 
      } 
     } 

    }); 

} 

} 

activity_main.xml中文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button - Go to mkyong.com" /> 

</LinearLayout> 

AndroidManifest.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.ahsankhan.cache" > 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 
+0

你会得到什么错误? *甚至是一个有效的变量名称? –

+1

[Android:Clear Cache of All Apps?]可能的重复?(http://stackoverflow.com/questions/14507092/android-clear-cache-of-all-apps) – EvenLisle

+0

@kevin工作人员不能执行变量名称,未知表达式第36行中的类型错误,37,42,43,48,49 – Andrew

回答

0

看看这一行:

public void onCreate(Bundle *) { 
    super.onCreate(*); 

您不能将*用作变量名称。它必须是这样的东西:

public void onCreate(Bundle b) { 
    super.onCreate(b); 
相关问题