title: Android AsyncTask execute every x minutes
body: <p>I have an AsyncTask what ping some server, I also create a Timer but when I try to execute I had an error</p>
  
  <pre><code>02-25 02:13:42.645  22313-22313/info.senyk.ustat E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: info.senyk.ustat, PID: 22313
  java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once)
          at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:579)
          at android.os.AsyncTask.execute(AsyncTask.java:535)
          at info.senyk.ustat.StatsActivity$PingTimer$1$1.run(StatsActivity.java:190)
          at android.os.Handler.handleCallback(Handler.java:739)
          at android.os.Handler.dispatchMessage(Handler.java:95)
          at android.os.Looper.loop(Looper.java:155)
          at android.app.ActivityThread.main(ActivityThread.java:5696)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
  </code></pre>
  
  <p>My Activity</p>
  
  <pre><code>public class StatsActivity extends ActionBarActivity {
  
  View mDecorView;
  
  String Login = "0";
  String PWD = "0";
  String Hostname = "0";
  String Port = "0";
  
  String PingResult= "";
  
  
  PingAsync pingasync;
  Ping ping = new Ping();
  
  
  TextView InfoPing;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_stats);
  
      mDecorView = getWindow().getDecorView();
  
      Login = getIntent().getExtras().getString("Login");
      PWD = getIntent().getExtras().getString("PWD");
      Hostname = getIntent().getExtras().getString("Hostname");
      Port = getIntent().getExtras().getString("Port");
  
      TextView InfoServerId = (TextView) findViewById(R.id.textViewServerId);
      InfoServerId.setText(Login + ":" + PWD + "@" + Hostname + ":" + Port);
  
      InfoPing = (TextView) findViewById(R.id.textViewPing);
  
      pingasync = new PingAsync();
      pingasync.execute();
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_stats, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
      super.onWindowFocusChanged(hasFocus);
      if (hasFocus) {
          mDecorView.setSystemUiVisibility(
                  View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                          | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                          | View.SYSTEM_UI_FLAG_FULLSCREEN
                          | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);}
  }
  
  public void click(View view) {
      PingTimer pingTimer = new PingTimer();
      pingTimer.startTimer();
  
  }
  
  
  class PingAsync extends AsyncTask&lt;String, Void, String&gt; {
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          InfoPing.setText("Ping ...");
      }
  
      @Override
      protected String doInBackground(String... params) {
  
          PingResult = ping.ping(Hostname);
  
          return PingResult;
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          InfoPing.setText(result);
  
      }
  
  }
  
  
  public class PingTimer extends ActionBarActivity {
  
      Timer timer;
      TimerTask timerTask;
  
      final Handler handler = new Handler();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_stats);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          startTimer();
      }
  
      public void startTimer() {
          timer = new Timer();
  
  
          initializeTimerTask();
          timer.schedule(timerTask, 5000, 10000);
      }
  
      public void stoptimertask(View v) {
          if (timer != null) {
              timer.cancel();
              timer = null;
          }
      }
  
      public void initializeTimerTask() {
  
          timerTask = new TimerTask() {
              public void run() {
                  handler.post(new Runnable() {
                      public void run() {
  
                          pingasync.execute();
  
  
                      }
                  });
              }
          };
      }
  }
  </code></pre>
  
  <p>As I understand that AsyncTask already executed but if I use</p>
  
  <p>InfoPing.setText(ping.ping(Hostname));</p>
  
  <p>instead</p>
  
  <p>pingasync.execute();</p>
  
  <p>my Activity freeze executing ping.ping(Hostname)</p>
  
  <p>Any ideas how to fix that?</p>