我正在尝试删除所有.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();
}
}
}
}
}
我有3场演出
答案 0 :(得分:0)
在后台Thread
或AsnycTask
doInBackground
方法中调用此语句
walkdir(dir);
<强> 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);
}