2013-07-12 3 views
-1

Я хочу нажать на предпочтение, чтобы очистить кеш моего приложения. Я сделал это, но это не работает, и есть ошибки. Как я могу исправить?Clear Cache App: Почему это не работает?

Это целый источник. Большое спасибо!!!!!!

public class Impo extends PreferenceActivity{ 


Preference info; 
Intent intent; 
Preference cache; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
this.addPreferencesFromResource(R.xml.layout); 


info= (Preference) this.findPreference("info"); 
info.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
    public boolean onPreferenceClick(Preference preference) { 
     intent = new Intent(getBaseContext(), Info.class); 
     startActivity(intent); 
     return true; 
    }}); 
cache = (Preference)this.findPreference("cache"); 
cache.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

} 
} 
public static void clearCache(final Context context) 
{ 
final File cache=context.getCacheDir(); 
final File appDir=new File(cache.getParent()); 
if(appDir.exists()) 
    { 
    // you might be able to change this whole code block to just "deleteDir(appDir)" 
    final String[] children=appDir.list(); 
    for(final String childFilePath : children) 
    if(!childFilePath.equals("lib")) 
     { 
     deleteDir(new File(appDir,childFilePath)); 
     Log.i("TAG","**************** File /data/data/APP_PACKAGE/"+childFilePath+" DELETED *******************"); 
     } 
    } 
} 

public static boolean deleteDir(final File dir) 
{ 
if(dir==null) 
    return true; 
if(dir.isDirectory()) 
    { 
    final String[] children=dir.list(); 
    for(final String childFilePath : children) 
    { 
    final boolean success=deleteDir(new File(dir,childFilePath)); 
    if(!success) 
     return false; 
    } 
    } 
return dir.delete(); 
} 
} 

.......................................

+0

"это не работает, и есть ошибки". Итак, расскажите нам, какие ошибки вы получите, сообщите нам, что вы звоните в Log show, расскажите нам, что не работает. – Yojimbo

+0

в порядке. Eclipse отмечает ошибку здесь File appDir = new File (cache.getParent()), здесь}, здесь public static boolean deleteDir (File dir), и здесь if (dir! = null & & dir.isDirectory()) { –

+0

do вы импортируете класс File? – VinceFR

ответ

0

вот мое предложение о том, как исправить ваши ошибки.

Я мог бы сделать это еще лучше, но он должен работать нормально.

cache.setOnPreferenceClickListener(new OnPreferenceClickListener() 
    { 
    clearCache(getApplicationContext()); 
    } 

и здесь функции, фиксированные:

public static void clearCache(final Context context) 
    { 
    final File cache=context.getCacheDir(); 
    final File appDir=new File(cache.getParent()); 
    if(appDir.exists()) 
    { 
    // you might be able to change this whole code block to just "deleteDir(appDir)" 
    final String[] children=appDir.list(); 
    for(final String childFilePath : children) 
     if(!childFilePath.equals("lib")) 
     { 
     deleteDir(new File(appDir,childFilePath)); 
     Log.i("TAG","**************** File /data/data/APP_PACKAGE/"+childFilePath+" DELETED *******************"); 
     } 
    } 
    } 

public static boolean deleteDir(final File dir) 
    { 
    if(dir==null) 
    return true; 
    if(dir.isDirectory()) 
    { 
    final String[] children=dir.list(); 
    for(final String childFilePath : children) 
     { 
     final boolean success=deleteDir(new File(dir,childFilePath)); 
     if(!success) 
     return false; 
     } 
    } 
    return dir.delete(); 
    } 

вот весь код класса:

public class Impo extends PreferenceActivity { 

    Preference info; 
    Intent intent; 
    Preference cache; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.addPreferencesFromResource(R.xml.layout); 

     info = this.findPreference("info"); 
     info.setOnPreferenceClickListener(new OnPreferenceClickListener() { 
      @Override 
      public boolean onPreferenceClick(final Preference preference) { 
       // TODO the next line is very weird. sure that's what you want? 
       intent = new Intent(getBaseContext(), Info.class); 
       startActivity(intent); 
       return true; 
      } 
     }); 
     cache = this.findPreference("cache"); 
     cache.setOnPreferenceClickListener(new OnPreferenceClickListener() { 

      @Override 
      public boolean onPreferenceClick(final Preference preference) { 
       clearCache(Impo.this); 
       return true; 
      } 
     }); 
    } 

    public static void clearCache(final Context context) { 
     final File cache = context.getCacheDir(); 
     final File appDir = new File(cache.getParent()); 
     if (appDir.exists()) { 
      // you might be able to change this whole code block to just "deleteDir(appDir)" 
      final String[] children = appDir.list(); 
      for (final String childFilePath : children) 
       if (!childFilePath.equals("lib")) { 
        deleteDir(new File(appDir, childFilePath)); 
        Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + childFilePath 
          + " DELETED *******************"); 
       } 
     } 
    } 

    public static boolean deleteDir(final File dir) { 
     if (dir == null) 
      return true; 
     if (dir.isDirectory()) { 
      final String[] children = dir.list(); 
      for (final String childFilePath : children) { 
       final boolean success = deleteDir(new File(dir, childFilePath)); 
       if (!success) 
        return false; 
      } 
     } 
     return dir.delete(); 
    } 
} 
+0

Я написал функции, функции не могут быть записаны внутри других функций. Положите «}» в правильные места, а затем проверьте его снова. Кроме того, то, что вы показали, не показывает ошибки, только их маркеры. Ошибки написаны в представлении «проблемы» eclipse (используйте ALT + SHIFT + Q, X, чтобы показать его). Я снова отредактировал свой ответ, так что вы видите, что они разделены. –

+0

Я положил из работ, но ошибки здесь ClearCache (getApplicationContext()); Множественные маркеры на этой линии \t - Синтаксическая ошибка, вставить «}», чтобы завершить ClassBody \t - ошибка синтаксиса, вставить «» завершить \t ConstructorDeclaration –

+0

ваш код не будет компилироваться, а не из-за написанных мной функций, а потому, что ваше местоположение написанный там. Посмотрите на строки после «cache.setOnPreferenceClickListener» - вместо того, чтобы реализовать интерфейс, который вы там поставили, я написал. опять же, они должны быть вне любого метода или функции. они не должны быть там, где вы их положили. –