0

I would like my android app to pause for a while after receive the resond from the server. I am making use of the following code to try to pause my program:

private Handler mHandler;
private Runnable mCountUpdater = new Runnable() {
    @Override
    public void run() {
        // TODO Auto-generated method stub
         mHandler.postDelayed(this, 15000); 
    } 
}; 

but no matter I put call the wait in either way mentioned below, it does not work
1. put inside the button handler:

btnin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    //set the values
    grab.onPreExecute("TechID", Build.SERIAL);
    grab.onPreExecute("Type", "Checkin");
    //set the destination and send the request
    grab.execute(new String[]{"http://192.168.1.150/Me/check.php"});
    //close the activity
    mHandler = new Handler(); 
    mHandler.post(mCountUpdater);
    finish();
    }
});

2.put in the onPostExecute() method of AsynTask:

private class GrabURL extends AsyncTask<String, Void, Void>{
//ArrayList object for storing the string pairs
ArrayList<NameValuePair> nameValuePairs;

public GrabURL() { 
    //constructor of the class
    nameValuePairs = new ArrayList<NameValuePair>(); 
} 

protected void onPreExecute(String key, String value) {
    //store the pair of values into the ArrayList 
    nameValuePairs.add(new BasicNameValuePair(key,value));
}

@Override
protected Void doInBackground(String... urls) {
      // TODO Auto-generated method stub
      //Operation being executed in another thread
      try{
          //set up the type of HTTPClient
          HttpClient client = new DefaultHttpClient();
          //set up the location of the server
          HttpPost post = new HttpPost(urls[0]);
          //translate form of pairs to UrlEncodedFormEntity 
          UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8);
          //set up the entity being sent by post method
          post.setEntity(ent);
          //execute the url and post the values
          //client.execute(post);
          HttpResponse responsePOST = client.execute(post); 
          HttpEntity resEntity = responsePOST.getEntity();
          line = EntityUtils.toString(resEntity);
    } catch (Exception e) {
        //catch the exception
        line = "Can't connect to server";
    }
    return null;
}

protected void onPostExecute(Void unused) {
        mRespond.setVisibility(View.VISIBLE); 
        mRespond.setText(line); 
        btnin.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext(), "Value updated", Toast.LENGTH_SHORT).show();
        mHandler = new Handler(); 
        mHandler.post(mCountUpdater);   
}
}

Is it the way I used to pause the program is incorrect or I have put the wait function in a wrong position?

Conrad
  • 933
  • 4
  • 19
  • 34

1 Answers1

1

I don't get what's the point to make a pause after you have received the response from the server. By the way, what do you mean exactly with "make a pause"? I can't imagine any reason to pause an application.

You may want to add a loader while (not after) the response from server is coming, and possibly, you may want to deactivate or hide buttons too. In that case, you should do that changes before grab.execute and remove changes in onPostExecute.

Moreover, I think that mCountUpdater is creating an infinite loop. So, I would rethink that code deeply. Take in to account, that Runnable.postDelay doesn't stop application by no means, it simply delays the execution of some Runnable by the specified time, but meanwhile, the application is still running.

I might help you more if you explain better the need of that solution.

Fran J Martínez
  • 236
  • 2
  • 12
  • Actually, my app simply get some data from the server and show to the user, and I would like it to close automatically after a while, say maybe 30 seconds, that's what I exactly want to do, I am sorry my explaination above is not clear enough. If I do not specify the condition in run(), then mCountUpdater will create infinite loop? – Conrad Jul 31 '12 at 01:27
  • When you say "I would like it to close automatically after a while", do you mean close application and return to phone's home, return to some previous activity or remove some UI element which shows the data? – Fran J Martínez Jul 31 '12 at 08:18
  • what I mean is to return to phone's home, that's why I use finish() initially, but later I have modified my decide and want to show some messages before ending the application, therefore I want to pause the application and make this question when I fail to do so. – Conrad Jul 31 '12 at 08:38
  • Here you have what you need: http://stackoverflow.com/questions/3724509/going-to-home-screen-programmatically – Fran J Martínez Jul 31 '12 at 09:28