我的线程不起作用

时间:2014-05-13 14:27:01

标签: android sensor

我正在尝试将我在网上找到的两个简单应用程序结合起来。我有一个线程,5秒后我的传感器列表应该显示一个Toast消息。但没有任何事情发生..线程不工作我觉得我搞砸了一切都好。请你帮忙。我真的很喜欢

public class MainActivity extends Activity{

    List<String>sName=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "Loadingg", Toast.LENGTH_LONG).show();

        Thread thr=new Thread(){
            @Override
            public void run (){

                try {
                    sleep(5000);
                    StringBuilder message=DisplaySensors();
                    Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();


                } catch (Exception e) {
                    // TODO: handle exception
                }


            }

                private StringBuilder DisplaySensors() {

                SensorManager sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
                List<Sensor>sList=sm.getSensorList(Sensor.TYPE_ALL);
                StringBuilder sb=new StringBuilder();
                for (int i = 0; i <sList.size(); i++) {
                    sb.append(((Sensor)sList.get(i)).getName()).append("\n");

                }

                return sb;

            }



        };
        thr.start();


    }

2 个答案:

答案 0 :(得分:1)

所有UI操作都必须在主UI线程上运行。因此,如果您想显示Toast消息,则不应在单独的Thread中完成。在这种情况下,toast消息必须位于runOnUiThread()块中,如下所示。

public class MainActivity extends Activity{

List<String>sName=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "Loadingg", Toast.LENGTH_LONG).show();

    Thread thr=new Thread(){
        @Override
        public void run (){

            try {
                sleep(5000);
                StringBuilder message=DisplaySensors();
                runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
                }
            });



            } catch (Exception e) {
                // TODO: handle exception
            }


        }

            private StringBuilder DisplaySensors() {

            SensorManager sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
            List<Sensor>sList=sm.getSensorList(Sensor.TYPE_ALL);
            StringBuilder sb=new StringBuilder();
            for (int i = 0; i <sList.size(); i++) {
                sb.append(((Sensor)sList.get(i)).getName()).append("\n");

            }

            return sb;

        }



    };
    thr.start();


}

答案 1 :(得分:1)

您不应在Toast中使用Thread。请改用runOnUiThread

请参阅this

@Override
public void run (){

    try {
        Thread.sleep(5000);
        StringBuilder message=DisplaySensors();
        runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                         Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
                    }
                });

    } catch (Exception e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
                e.printStackTrace();
            }
}