title: limit the dialog inteval in android
body: <p>I am making a call to server from my android app. I have done this using <strong>AsyncTask</strong> Thread.
  Now I have started Progress Dialog in <strong>onPreExecute()</strong> Method as follows:                         </p>
  
  <pre><code>protected void onPreExecute(){ 
  dialog = new ProgressDialog(appcontext);
  dialog.setMessage("Sending Position...");
  dialog.setIndeterminate(true);
  dialog.setCancelable(false);
  dialog.show();
  }
  </code></pre>
  
  <p>Now, in <strong>doInBackground()</strong> method I have been doing my sending data tasks.And after I get the response I am going to <strong>onPostExecute()</strong>  method where I have used <strong>dialog.dismiss()</strong> to stop <strong>ProgressDialog</strong>.</p>
  
  <p><strong>my doInBackGround() code</strong></p>
  
  <pre><code>protected String doInBackground(Position... position){
      InputStream inputStream = null;
      String temp=null;
  HttpClient httpclient = new DefaultHttpClient();
  HttpPost httpPost = new HttpPost("http://10.0.2.2:8084/WebApplication5/getData");
  String json = "";
  JSONObject jsonObject = new JSONObject();
  try { 
       jsonObject.accumulate("time",position[0].getTime()); 
       json = jsonObject.toString();
       StringEntity se = new StringEntity(json);
       httpPost.setEntity(se);
       httpPost.setHeader("Accept", "application/json");
       httpPost.setHeader("Content", "application/json");
       org.apache.http.HttpResponse httpResponse = httpclient.execute(httpPost);
  
    **// CODE ACTUALLY BLOCKS HERE UNTIL I GET A RESPONSE.(WHAT I THINKS). THOUGH WORKS 99%** 
  
       inputStream = httpResponse.getEntity().getContent();
       if(inputStream!=null)
            temp="sent"; 
   }
   catch (JSONException e){
       e.printStackTrace();
   }
   catch (IllegalStateException e) {
       e.printStackTrace();
       } 
   catch (IOException e) {
       e.printStackTrace();
       }
      return temp;
  }
  </code></pre>
  
  <p>ALL IS FINE but sometimes I do not get a response (let it be any server error), In that case the dialog is unable to dismissed and is running undefinitely. I want to limit the duration of <strong>doInBackGround()</strong> method for few seconds. So that even in case of no response it will move to <strong>onPostExecute()</strong> and my infinte going dialog should stop.</p>
  
  <p>Secondly, In case I get a response I want to save it to my SharedPreferences, so my question is what is the best way to do that, i.e.</p>
  
  <p>1) I should do it in <strong>onPause()</strong> method.</p>
  
  <p>2) or, in <strong>onStop()</strong> method,</p>
  
  <p>3) or in <strong>onPostExecut()e</strong> method</p>
  
  <p>or there is no point in that, I can do it anywhere.</p>
  
  <p>And my last question is what is the what is the lifetime of data that is stored in Shared Preferences.</p>