AsyncTask的用法:[url]http://www.cnblogs.com/dawei/archive/2011/04/18/2019903.html[/url]
初探异步 AsyncTask 扫描SD卡:[url]http://www.eoeandroid.com/thread-113400-1-1.html[/url]
初探异步 AsyncTask 扫描SD卡:[url]http://www.eoeandroid.com/thread-113400-1-1.html[/url]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入文件类型"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始扫描" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</LinearLayout>
package com.pandy.task;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class AsyncTaskDemoActivity extends Activity {
/** Called when the activity is first created. */
private Button button1;
private EditText editText1;
private List<String> list;
private TextView textView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTitle("Pandy:扫描SD卡的文件类型.");
button1 = (Button)findViewById(R.id.button1);
editText1 = (EditText)findViewById(R.id.editText1);
textView1 = (TextView)findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(editText1.getText()==null||editText1.getText().toString().trim().length()==0){
Toast.makeText(AsyncTaskDemoActivity.this, "请输入文件类型...",Toast.LENGTH_SHORT).show();
return;
}
list = new ArrayList<String>();
new AsyncTask<Integer,Integer,Integer>(){
private ProgressDialog dialog;
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
//执行完成之后
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
dialog.dismiss();
super.onPostExecute(result);
Toast.makeText(AsyncTaskDemoActivity.this, "执行完成...",Toast.LENGTH_SHORT).show();
textView1.setText("");
if(list!=null){
for(int i=0; i<list.size(); i++){
textView1.setText(textView1.getText()+list.get(i)+"\n");
}
}
}
//执行之前
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = ProgressDialog.show(AsyncTaskDemoActivity.this, "Title", "Load......");
super.onPreExecute();
}
//修改界面
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
//这里开始启动进程
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub
//textView1.setText(""); //为什么放在这里就会出错????
String txt = editText1.getText()==null?"":editText1.getText().toString();
if(txt.length()<=0) {
Toast.makeText(AsyncTaskDemoActivity.this, "没有输入文件类型", Toast.LENGTH_LONG);
return null;
}
String status = android.os.Environment.getExternalStorageState();
if(android.os.Environment.MEDIA_MOUNTED.equalsIgnoreCase(status)){
File file = android.os.Environment.getExternalStorageDirectory();
scanSDCard(file,txt);
return null;
}else{
Toast.makeText(AsyncTaskDemoActivity.this, "不能访问SD卡.", Toast.LENGTH_LONG);
return null;
}
}
}.execute(0);
}
});
}
public void scanSDCard(File file,String ext){
if(file.isDirectory()){
File[] files = file.listFiles();
if(files!=null){
for(int i=0; i<files.length; i++){
File tmp = files[i];
if(tmp.isFile()){
String fileName = tmp.getName();
if(fileName.indexOf(".")>=0){
fileName = fileName.substring(fileName.lastIndexOf(".")+1);
if(ext!=null&&ext.equalsIgnoreCase(fileName))
list.add(tmp.getAbsolutePath());
}
}else
scanSDCard(tmp,ext);
}
}
}else{
if(file.isFile()){
String fileName = file.getName();
if(fileName.indexOf(".")>=0){
fileName = fileName.substring(fileName.lastIndexOf(".")+1);
if(ext!=null&&ext.equalsIgnoreCase(fileName))
list.add(file.getAbsolutePath());
}
}
}
}
}