runonuithread在android中不起作用

时间:2013-02-05 10:41:38

标签: android

我的工作任务是读取SD卡文件,我想在textview上显示所有带位置的SD卡文件但是,我的代码读取成功但是,我找不到解决方案后读取文件时无法更新textview文本runonuithread但是,它也没有工作。我的代码如下所示:

public void scan (final File path) {

            try
            {
                for (final File f : path.listFiles()) {
                    if (f.isFile()) {
                      scannedfiles=scannedfiles+1;
                      progresspercentage=scannedfiles*100/FilesCount;   


                      MainActivity.this.runOnUiThread(new Runnable() {

                          @Override
                          public void run() {
                              lbl_scan.setText("SCANING:"+f.getAbsolutePath());\\first time only change text after didn't change
                              Toast.makeText(getApplicationContext(),"Files:"+f.getAbsolutePath(),Toast.LENGTH_SHORT).show(); 
                          }
                      });


                    }
                    else {
                        if(!f.getName().equals("Android"))
                        {
                            scan(f);
                        }
                    }
                }

            }
            catch (Exception e) 
            {

            }
        }

我在代码下面调用扫描功能:

 File root = new File (Environment.getExternalStorageDirectory().getAbsolutePath());
 scan(root);

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:2)

您可能正在UI线程上调用扫描!如果整个UI在操作期间阻塞,这应该是显而易见的。

尝试:

new Thread() {
    public void run() {
        scan(root);
    }
}.start();
相关问题