Question

I am trying to combine two simple application that I found on the net.I have a thread and after 5 seconds my sensor lists should be displayed with a Toast message.But Nothing happens ..Thread is not working I think I messed up everything.Could you please help. I would really appriciate

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();


    }
Was it helpful?

Solution

All UI operations have to run on Main UI Thread. So if you want to show a toast message, this shouldn't done in a seperated Thread. In this case, toast message has to be in runOnUiThread() block as seen below.

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();


}

OTHER TIPS

You should not use the Toast in a Thread. Use runOnUiThread instead:

See 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();
            }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top