-1

In my code i want to check longitude and latitude value. If values are empty, code will check for 15 seconds. If values are received , while loop will break and will show "You win". If they are still empty, it should show "error in gps" after 15 seconds. The problem is that code is waiting perfectly but Progress Bar is not showing in the screen because screen got stuck during wait time. After 15 seconds screen is free. Please tell me how to resolve this issue. Thanks

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(getBaseContext(), longitude.getText(), Toast.LENGTH_LONG).show();
                 Toast.makeText(getBaseContext(), latitude.getText(), Toast.LENGTH_LONG).show();
                pBar = new ProgressDialog(oproprietor.this);
                pBar.setCanceledOnTouchOutside(false);
                pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pBar.setMessage("Waiting 15 seconds");
                pBar.show();

                 long t= System.currentTimeMillis();
                 long end = t+15000;
                 while(System.currentTimeMillis() < end) {


                        if(!longitude.getText().equals("") || !latitude.getText().equals("") ){

                           pBar.dismiss();
                                break;
                                }
                        else continue;


                 }
                 if(longitude.getText().equals("") || latitude.getText().equals("")){

                             pBar.dismiss();
                        Toast.makeText(oproprietor.this, "Error in gps", Toast.LENGTH_LONG).show();



                        }
                 else if(!longitude.getText().equals("") || !latitude.getText().equals("") ){

                     pBar.dismiss();
                     Toast.makeText(oproprietor.this, "You win", Toast.LENGTH_LONG).show();
                        }
            }

         });
user3118495
  • 11
  • 1
  • 6
  • I really wonder what people think the `// TODO Auto-generated method stub` comment that their IDE generates is for. Are they afraid that if they take it out, the code will break? Do they just not see it? – David Conrad Sep 10 '14 at 16:12
  • I personally feel it makes the code look much cleaner but thats just my opinion. – vishalmullur Sep 10 '14 at 16:14

2 Answers2

2

Since this is Android, I would definitely recommend using an AsyncTask over a thread. This question has all the relevant details on how to do this safely. Using a Java thread/Runnable introduces some UI issues in android when you are interacting with the UI thread from the external thread.

Community
  • 1
  • 1
emerssso
  • 2,376
  • 18
  • 24
0

Try running it in a thread.

// Note: declare ProgressDialog progress as a field in your class.

progress = ProgressDialog.show(this, "dialog title",
  "dialog message", true);

new Thread(new Runnable() {
  @Override
  public void run()
  {
    // do the thing that takes a long time

    runOnUiThread(new Runnable() {
      @Override
      public void run()
      {
        progress.dismiss();
      }
    });
  }
}).start();
erad
  • 1,766
  • 2
  • 17
  • 26