我要实现的功能是检索SDcard里的所有txt文件
我尝试过很多办法
1 不用线程 直接在主线程里检索文件 由于有循环且检索时间会超过5秒 导致黑屏然后提示未响应
2 我建立一个线程让线程去检索文件当检索完毕后把检索到的文件传递给主类 主类再把检索到的文件显示在list上 但是当线程一启动程序就崩溃了
3 最后我在主类里添加handler 当子线程检索完毕就sendmessage给主类 然后主类把子线程检索到的文件显示在list上但程序还是崩溃 我没在子线程里操作UI
好像只要子线程里有循环程序就会崩溃
下面我把代码贴出来希望大家能帮帮我解决下这个问题
主类的主要代码
enter code here int jishuqi = 0;
ProgressDialog progress;
jiansuothread thread;
Intent intent;
int[] leixing_xuanzhe;
FileReader file;
File fuwenjian;
File fuwenjianxia[];
String[] leixing = new String[] {"doc","pdf","xls","txt","jpg","ppt"};
int i = 0;
ArrayList<File[]>root = new ArrayList<File[]>();
String xuanxiang [] = new String [] {"向上","编辑","添加","删除"};
int ico [] = new int[] {R.drawable.xinjian,R.drawable.xinjian,R.drawable.xinjian,R.drawable.xinjian};
ListView list;
Button xiangshang;
Button bianji;
Button tianjia;
Button shanchu;
View v;
static ArrayList<File[]>jiansuoroot = new ArrayList<File[]>();
static List<File>jiansuowenjian = new ArrayList<File>();
static File fi;
static ArrayList<File>wenjianjia = new ArrayList<File>();
static ArrayList<File>ziwenjian = new ArrayList<File>();
static ArrayList<File[]>ziwenjian1 = new ArrayList<File[]>();
static ArrayList<File[]>rootwenjianjia = new ArrayList<File[]>();
//jiansuowen js = new jiansuowen();
//zixiancheng xiancheng = null;
Handler hanlder;
Handler handler = null;
Handler childhandler = null;
boolean jiansuowanbi = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_text);
xiancheng = new zixiancheng();
xiancheng.start();
v = (View)findViewById(R.id.view1);
intent = this.getIntent();
leixing_xuanzhe = intent.getIntArrayExtra("leixing_xuanzhe");
list = (ListView)findViewById(R.id.fileslist);
parse(leixing_xuanzhe);
list.setOnItemClickListener(new mylistitemclick());
xiangshang = (Button)findViewById(R.id.wj_xiangshang);
bianji = (Button)findViewById(R.id.wj_bianji);
tianjia = (Button)findViewById(R.id.wj_tianjia);
shanchu = (Button)findViewById(R.id.wj_shanchu);
xiangshang.setOnClickListener(new myClick());
bianji.setOnClickListener(new myClick());
tianjia.setOnClickListener(new myClick());
shanchu.setOnClickListener(new myClick());
//thread = new jiansuothread(this);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 111) {
progress.cancel();
List<Map<String,Object>>listitems = new ArrayList<Map<String,Object>>();
for(int i= 0;i<jiansuowenjian.size();i++) {
Map<String,Object>item = new HashMap<String,Object>();
item.put("icon",R.drawable.file );
item.put("name", jiansuowenjian.get(i).getName());
listitems.add(item);
}
SimpleAdapter adpter = new SimpleAdapter(TexlisttActivity.this,listitems , R.layout.list, new String[] {"icon","name"}, new int[] {R.id.icon,R.id.listitem});
list.setAdapter(adpter);
}
}
};
}`
子线程的代码
enter code here`
class zixiancheng extends Thread {
Message msg = null;
public void run() {
Looper.prepare();
childhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 222) {
try {
for(int i =0; i<wenjianjia.size();i++) {
jinasuo1(wenjianjia.get(i));
}
jiansuowanbi = true;
msg = handler.obtainMessage();
msg.what = 111;
handler.sendMessage(msg);
} catch (Exception e) {
// TODO: handle exception
}
}
}
};
Looper.loop();
}
}
ChatGPT尝试为您解答,仅供参考
在 Android 应用中,主线程(也称为 UI 线程)负责更新界面,如果在主线程中执行耗时任务,会导致界面卡顿甚至假死,因此,我们通常需要在另一个线程中执行耗时任务,最后在主线程中更新界面。
您可以尝试使用 AsyncTask 类来执行耗时任务,该类提供了一种简单的方法来将耗时任务与 UI 线程分离开来。
下面是一个使用 AsyncTask 类执行耗时任务的示例代码:
private class SearchTask extends AsyncTask<Void, Void, List<File>> {
@Override
protected List<File> doInBackground(Void... params) {
// 在这里执行耗时任务,例如搜索 SD 卡中的文件
List<File> files = searchFilesInSDCard();
return files;
}
@Override
protected void onPostExecute(List<File> files) {
// 当耗时任务执行完毕后,在 UI 线程中更新界面
updateUI(files);
}
}
您可以在主线程中调用如下代码启动异步任务:
new SearchTask().execute();
请注意,在 Android 中,不能在子线程中更新 UI。如果需要在子线程中更新 UI,可以使用 Handler 类。
希望这些信息能够帮助您解决问题。