简体   繁体   中英

Calling a new Activity from within Runnable

I have a flow to handle in andoid :

Suppose I am in Activity 1 and then there is one API say https://status.com/key which tells me whether I need to navigate to Activity 2 or stay on Activity 1 itself.

So in Activity-1 I have put the HttpRequest call inside runnable thread and I am continuously pinging that API after every second to know its response. The moment I get a response "yes" I navigate to Activity-2 (Google Maps Activity).

But what I noticed is even after navigating to Activity-2, the Runnable thread in Activity-1 is continuously running and as a result of which the Activity-2(Google maps activity) keeps loading again and again undesirably. How do I stop the runnable thread in Activity-1 :

  1. Tried Thread.stop() // Not working
  2. System.exit(0) //is a bad option
final Runnable r = new Runnable() {
            public void run() {
                String myUrl="https://app.herokuapp.com/users/userredirection-pickup/"+ridetrackingno;
                Log.d("URL",myUrl);
                HttpGetRequest getRequest = new HttpGetRequest();
                try{
                    String result = getRequest.execute(myUrl).get();
                    JSONArray j=new JSONArray(result);
                    JSONObject jo= j.getJSONObject(0);
                    String r=jo.getString("status");
                    if(r.equals("yes")){
                        Intent show = new Intent (getApplicationContext(), Riding.class);
                        show.putExtra("Username",username);
                        show.putExtra("Phone",phone);

                        startActivity(show);
                    }
                } catch(Exception e)
                {
                    e.printStackTrace();
                }
                handler.postDelayed(this, 2000);
            }
        };

        handler.postDelayed(r, 2000);

Please help. Thanks!

try this. Maybe it will work

final Runnable r = new Runnable() {
            public void run() {
                String myUrl="https://app.herokuapp.com/users/userredirection-pickup/"+ridetrackingno;
                Log.d("URL",myUrl);
                HttpGetRequest getRequest = new HttpGetRequest();
                try{
                    String result = getRequest.execute(myUrl).get();
                    JSONArray j=new JSONArray(result);
                    JSONObject jo= j.getJSONObject(0);
                    String r=jo.getString("status");
                    if(r.equals("yes")){
                        try {
                             runOnUiThread(new Runnable() {

                             @Override
                             public void run() {
                                 Intent show = new Intent (getApplicationContext(), Riding.class);
                                 show.putExtra("Username",username);
                                 show.putExtra("Phone",phone);

                                 startActivity(show);
                    }
                });
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

                    }
                } catch(Exception e)
                {
                    e.printStackTrace();
                }
                handler.postDelayed(this, 2000);
            }
        };

        handler.postDelayed(r, 2000);

What I was saying was this

if(r.equals("yes")){
    Intent show = new Intent (getApplicationContext(), Riding.class);
    show.putExtra("Username",username);
    show.putExtra("Phone",phone);

    startActivity(show);
} else {
    handler.postDelayed(this, 2000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM