// 搜索文件的异步任务
new AsyncTask<Integer, Integer, String>() {
private ProgressDialog dialog;
// UI显示
protected void onPreExecute() {
Log.i(TAG, "onPreExecute Thread id "+Thread.currentThread().getId());
dialog = ProgressDialog.show(
context, "",
"正在扫描SD卡,请稍候....");
super.onPreExecute();
}
// 后台执行
protected String doInBackground(Integer... params) {
Log.i(TAG, "doInBackground Thread id "+Thread.currentThread().getId());
if (!android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED)) {
} else {
if (!editText.getText().toString().equals("")) {
filelist.clear();
return Search_Files(Environment
.getExternalStorageDirectory());
}
}
return null;
}
// 搜索完毕后,结果处理
protected void onPostExecute(String result) {
Log.i(TAG, "onPostExecute Thread id "+Thread.currentThread().getId());
dialog.dismiss();
if (editText.getText().toString().equals("")) {
Toast.makeText(context,
"请输入搜索的文件名", 1000).show();
} else {
new AlertDialog.Builder(context)
.setTitle("SD卡搜索结果")
.setMessage(result)
.create().show();
}
super.onPostExecute(result);
}
}.execute(0);
搜索文件的异步任务 new AsyncTask
最新推荐文章于 2020-05-18 15:12:30 发布