设备在删除文件时冻结

时间:2016-03-28 06:09:34

标签: android

我正在尝试删除所有.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场演出

1 个答案:

答案 0 :(得分:0)

在后台ThreadAsnycTask doInBackground方法中调用此语句

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

}