2016-09-29 10 views
0

私のアプリではリアルタイムのトラッキングが必要なので、5秒ごとにトリガーする必要があるボタンが必要ですが、どうすればいいのかわかりません。どうやって教えてくれますか?5秒ごとにトリガーするボタン

5秒ごとにAsyncTaskがトリガーされます。

btnStart.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     HashMap postLoc = new HashMap(); 
     postLoc.put("txtLat", tvLat.getText().toString()); 
     postLoc.put("txtLng", tvLong.getText().toString()); 
     postLoc.put("txtOwner", pref.getString("username","").toString()); 

     PostResponseAsyncTask taskLoc = new PostResponseAsyncTask(getActivity(), postLoc,false, new AsyncResponse() { 
      @Override 
      public void processFinish(String s) { 
       Log.d(TAG, tvLat.getText().toString()); 
       Log.d(TAG, tvLong.getText().toString()); 
       Intent i = new Intent(getActivity(),GPS_Service.class); 
       getActivity().startService(i); 
      } 
     }); 

     taskLoc.execute("http://carkila.esy.es/carkila/locationUpdate.php"); 
    } 
}); 
+3

http://stackoverflow.com/questions/6531950/how-to-execute-async-task-repeatedly-after-fixed-time-intervals –

答えて

1

を、私は、このコードは、コードをトリガするために役に立つかもしれないと思う、すべての5秒

Timer timer; 
TimerTask timerTask; 
final Handler handler = new Handler(); 
@Override 
public void onCreate() { 
    super.onCreate();    
    startTimer(); 
} 

public void startTimer() { 
    //set a new Timer 
    timer = new Timer(); 

    //initialize the TimerTask's job 
    initializeTimerTask(); 

    timer.schedule(timerTask, 0, 5000); 
} 

public void initializeTimerTask() { 
    timerTask = new TimerTask() { 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        //code to run after every 5 seconds 
       } 
      }); 
     } 
    }; 
} 
+0

'handler.post'上の' post'は赤色の色です.... –

+0

私はちょうど間違ったハンドラを宣言しました。そのために残念。 hehe :) –

+0

その大丈夫心配しないでください!それがうまくいきたいと思っています –

0

このような方法を作成し、ボタンクリックでメソッドを呼び出しても、このようにハンドラを使用してメソッドを呼び出します。

mRunnable = new Runnable() { 
     public void run() { 
      public void toBecalled_Every_5_Second(); 
      mHandler.postDelayed(mRunnable, 5000); 
     } 
    }; 
mHandler.postDelayed(mRunnable, 5000); 


public void toBecalled_Every_5_Second(){ 
PostResponseAsyncTask taskLoc = new PostResponseAsyncTask(getActivity(), postLoc,false, new AsyncResponse() { 
       @Override 
       public void processFinish(String s) { 
        Log.d(TAG, tvLat.getText().toString()); 
        Log.d(TAG, tvLong.getText().toString()); 
        Intent i = new Intent(getActivity(),GPS_Service.class); 
        getActivity().startService(i); 
       } 
      }); 
       taskLoc.execute("http://carkila.esy.es/carkila/locationUpdate.php"); 
} 

ので、それはすべての5秒メソッドを呼び出し、Aになります同期タスクが実行されます....

+0

'mHandler.postDelayed'の' postDelayed'は赤の色です.... –

0

私はCountDownTimerを持っています。これは、5秒ごとにボタンクリック機能を起動します。

CountDownTimer mTimer = new CountDownTimer(50000, 1000) { 

    public void onTick(long millisUntilFinished) { 
     // Do nothing 
    } 

    public void onFinish() { 
     btnStart.performClick(); 
     this.start(); // Restart 
    } 
}.start(); 
+0

タイマーを再起動するのに必要な時間は、次のようになります:btnStart.performClick()times + 5 s。 –

0

あなたのUIすなわちメインスレッドに結果を更新するTimerTaskをハンドラタイマーを使用することができます。

このような何か:

Timer timer; 
TimerTask timerTask; 
//we are going to use a handler to be able to run in our TimerTask 
final Handler handler = new Handler(); 

private void initializeTimerTask() { 
    timerTask = new TimerTask() { 
    public void run() { 
     //use a handler to run process 
     handler.post(new Runnable() { 
     public void run() { 
      /**************************/ 
      /** Do your process here **/ 
      /**************************/ 
     } 
     }); 
    } 
    }; 
} 

private void startTimer() { 
    //set a new Timer 
    timer = new Timer(); 
    //initialize the TimerTask's job 
    initializeTimerTask(); 
    //schedule the timer, start run TimerTask then run every 5000ms i.e 5 seconds. 
    timer.schedule(timerTask, 0, 5000); // 
} 

private void stopTimerTask() { 
    //stop the timer, if it's not already null 
    if (timer != null) { 
    timer.cancel(); 
    timer = null; 
    } 
} 

Handler.post()であなたの処理コードを挿入します。その後、startTimer()を呼び出してトリガーを開始します。トリガーを停止するには、stopTimerTask()に電話してください。

+0

投稿者:handler.post投稿は赤色です。 –

+0

'import android.os.Handler;'を追加しましたか? –

関連する問題