2016-03-28 54 views
1

我想刪除所有的.apk文件。這工作正常!但是,它使我的設備速度變得很慢,一切都凍結了一會兒。有時它只是從記憶中消失。我如何使這個簡單的過程成本更低?設備凍結,同時刪除文件

setContentView(R.layout.activity_main); 

    // getting SDcard root path 
    File dir = new File(Environment.getExternalStorageDirectory() 
      .getAbsolutePath()); 
    walkdir(dir); 
} 

public void walkdir(File dir) { 
    String Patternjpg = ".apk"; 
    File listFile[] = dir.listFiles(); 
    if (listFile != null) { 
     for (int i = 0; i < listFile.length; i++) { 
      if (listFile[i].isDirectory()) { 
       walkdir(listFile[i]); 
      } else { 
       if (listFile[i].getName().endsWith(Patternjpg)) { 
        // Do what ever u want 
        listFile[i].delete(); 
       } 
      } 
     } 
    } 
} 

以及我的RAM 3場演出

回答

0

調用此聲明在背景ThreadAsnycTaskdoInBackground方法

walkdir(dir); 

EDIT1
LinkOne
LinkTwo

EDIT2:

這是我的項目中的AsyncTask之一。如果需要,您應該根據您的要求進行定製!

class ExplorerDBOperation extends AsyncTask<Void,Boolean,Boolean>{ 
    ProgressDialog dialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      dialog = ProgressDialog.show(HomeActivity.this, "Please wait", "Processing........."); 
     } 
@Override 
     protected Boolean doInBackground(Void... params) { 
String Patternjpg = ".apk"; 
    File listFile[] = dir.listFiles(); 
    if (listFile != null) { 
     for (int i = 0; i < listFile.length; i++) { 
      if (listFile[i].isDirectory()) { 
       walkdir(listFile[i]); 
      } else { 
       if (listFile[i].getName().endsWith(Patternjpg)) { 
        // Do what ever u want 
        listFile[i].delete(); 
       } 
      } 
     } 
    } 
return true; 
} 
@Override 
     protected void onPostExecute(Boolean aBoolean) { 
      super.onPostExecute(aBoolean); 
      if(dialog!=null && dialog.isShowing()) 
      {dialog.dismiss();} 


     } 
} 

在你onCreate方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// at the end of oncreate 
new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       new ExplorerDBOperation().execute(); 
      } 
     }, 300); 

} 
+0

哎,任何片段?謝謝!!:D – Emily

+0

也。它可以檢測任務何時完成? – Emily

+0

檢查我的編輯,包含的鏈接 – Stallion