Laurynas G
Laurynas G

Reputation: 607

Using wait() function in Android App

I am trying to simulate changing GPS location by simulating it. I am trying to change gps variable manually every second. I have tried various methods, but neither of them looks to work for me. Maybe someone will spot an error in my code? Most of the time screen just goes blank and activity doesn't even load.

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Larry on 18/12/2015.
 */
public class travel extends AppCompatActivity {
    float gps = 0;

    Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.travel_layout2);
        String[] words = getResources().getStringArray(R.array.information);
        String[] left = getResources().getStringArray(R.array.left);
        String[] right = getResources().getStringArray(R.array.right);
        String[] bubble = getResources().getStringArray(R.array.bubble);
        Spinner spinner1 = (Spinner) findViewById(R.id.beginning);
        Global app = Global.getInstance();
        String beginning=app.getValue(0);
        String ending=app.getValue(1);
        int begin = 1;
        int end = 1;
        int resId;
        int y=0;
        float latBegin=0;
        float latEnd=0;
        float lonBegin=0;
        float lonEnd=0;
        String name;
        TextView test;
        ImageView image;
        RelativeLayout layout;


        // deleted some code which initializes the page

        gps = latBegin;
        while (gps < latEnd) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            gps = gps + 0.001f;
            //Handler h = new Handler();

            // h.postDelayed(new Runnable() {

            //   @Override
            //   public void run() {
            // DO DELAYED STUFF
            //}, 1000);


            //SystemClock.sleep(1000);




            // Thread.sleep(1000);


            // Handler handler = new Handler();
            //handler.postDelayed(new Runnable() {
            //public void run() { }, 500);
            //});
            //setTimeout(onCreate(){ },1000);
        }
    }
}

Upvotes: 0

Views: 562

Answers (1)

koral
koral

Reputation: 2533

You see blank screen because you are doing time consuming operations on UI thread. You can use something like this to execute piece of code periodically, instead of while loop:

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //modify value
            //do something with new value
            handler.postDelayed(this, 500);
        }
    }, 500);

Upvotes: 1

Related Questions