Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1386111
  • 博文数量: 188
  • 博客积分: 1784
  • 博客等级: 上尉
  • 技术积分: 2772
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-05 22:20
个人简介

发上等愿,结中等缘,享下等福;择高处立,就平处坐,向宽处行。

文章分类

全部博文(188)

文章存档

2020年(12)

2019年(11)

2018年(4)

2017年(3)

2016年(11)

2015年(22)

2014年(19)

2013年(25)

2012年(32)

2011年(49)

分类: 嵌入式

2012-04-18 13:56:11

初探异步 AsyncTask

 

        最基本的异步DEMO,AsyncTask实现扫描SD卡指定后缀类型的文件(递归扫描)

 

 

  1. package allen.test;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import android.app.Activity;  
  8. import android.app.ProgressDialog;  
  9. import android.content.Intent;  
  10. import android.os.AsyncTask;  
  11. import android.os.Bundle;  
  12. import android.os.Environment;  
  13. import android.view.View;  
  14. import android.widget.Button;  
  15. import android.widget.EditText;  
  16. import android.widget.Toast;  
  17.   
  18. public class TestActivity extends Activity  
  19. {  
  20.   
  21.     static List txtList;// 文件列表   
  22.   
  23.     EditText editText;  
  24.   
  25.     public void onCreate(Bundle savedInstanceState)  
  26.     {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.   
  30.         this.setTitle("异步加载DEMO(扫描SD卡指定后缀文件)");  
  31.         Button btnPhoto = (Button) findViewById(R.id.button);  
  32.         editText = (EditText) findViewById(R.id.edittext);  
  33.         txtList = new ArrayList();  
  34.         btnPhoto.setOnClickListener(new View.OnClickListener()  
  35.         {  
  36.   
  37.             public void onClick(View v)  
  38.             {  
  39.   
  40.                 new AsyncTask()  
  41.                 {  
  42.   
  43.                     private ProgressDialog dialog;  
  44.   
  45.                     protected void onPreExecute()  
  46.                     {  
  47.                         dialog = ProgressDialog.show(TestActivity.this"",  
  48.                                 "正在扫描SD卡,请稍候....");  
  49.                         super.onPreExecute();  
  50.                     }  
  51.   
  52.                     protected String[] doInBackground(Integer... params)  
  53.                     {  
  54.                         if (!android.os.Environment.getExternalStorageState()  
  55.                                 .equals(android.os.Environment.MEDIA_MOUNTED))  
  56.                         {  
  57.   
  58.                         }  
  59.                         else  
  60.                         {  
  61.                             if (!editText.getText().toString().equals(""))  
  62.                             {  
  63.                                 GetFiles(Environment  
  64.                                         .getExternalStorageDirectory());  
  65.                             }  
  66.                         }  
  67.                         return null;  
  68.                     }  
  69.   
  70.                     protected void onPostExecute(String[] result)  
  71.                     {  
  72.                         dialog.dismiss();  
  73.                         if (editText.getText().toString().equals(""))  
  74.                         {  
  75.                             Toast.makeText(TestActivity.this"请输入有效信息",  
  76.                                     Toast.LENGTH_SHORT).show();  
  77.                         }  
  78.                         else  
  79.                         {  
  80.                             Toast.makeText(TestActivity.this"扫描完毕",  
  81.                                     Toast.LENGTH_LONG).show();  
  82.                             Intent in = new Intent();  
  83.                             in.setClass(TestActivity.this, ListOk.class);  
  84.                             TestActivity.this.startActivity(in);  
  85.                         }  
  86.                         super.onPostExecute(result);  
  87.                     }  
  88.                 }.execute(0);  
  89.             }  
  90.         });  
  91.   
  92.     }  
  93.   
  94.     /** 
  95.      * 获取文件列表 
  96.      *  
  97.      * @param filePath 
  98.      */  
  99.     public void GetFiles(File filePath)  
  100.     {  
  101.   
  102.         File[] files = filePath.listFiles();  
  103.   
  104.         if (files != null)  
  105.         {  
  106.             for (int i = 0; i < files.length; i++)  
  107.             {  
  108.                 if (files[i].isDirectory())  
  109.                 {  
  110.                     GetFiles(files[i]);  
  111.                 }  
  112.                 else  
  113.                 {  
  114.                     if (files[i].getName().toLowerCase()  
  115.                             .endsWith("." + editText.getText().toString()))  
  116.                     {  
  117.                         txtList.add(files[i]);  
  118.                     }  
  119.                 }  
  120.             }  
  121.         }  
  122.     }  
  123.   
  124. }  
     

 显示类

  1. package allen.test;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import android.app.ListActivity;  
  8. import android.os.Bundle;  
  9. import android.widget.ArrayAdapter;  
  10.   
  11. public class ListOk extends ListActivity  
  12. {  
  13.   
  14.     public void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.   
  18.         ArrayAdapter adapter = new ArrayAdapter(this,  
  19.                 android.R.layout.simple_list_item_1,  
  20.                 FileToStr(TestActivity.txtList));  
  21.         setListAdapter(adapter);  
  22.     }  
  23.   
  24.     /** 
  25.      * 把文件列表转换成字符串 
  26.      *  
  27.      * @param f 
  28.      * @return 
  29.      */  
  30.     public String[] FileToStr(List f)  
  31.     {  
  32.         ArrayList listStr = new ArrayList();  
  33.         for (int i = 0; i < f.size(); i++)  
  34.         {  
  35.             String nameString = f.get(i).getName();  
  36.             listStr.add(nameString);  
  37.         }  
  38.         return listStr.toArray(new String[0]);  
  39.     }  
  40. }  


 main布局

 

  1. xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/edittext"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:hint="请输入扫描的文件后缀名(例如mp3)" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/button"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="开始扫描SD卡" />  
  18.   
  19. LinearLayout>  

源代码下载:

阅读(1663) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~