2016-03-26 8 views
0

私はアンドゥン・タスクをAndroidで毎回実行したい。アンドロイドで連続して2回比較する

私の間隔は、ユーザーの選択に応じなど

.... = {15分、30分、1時間です。

私は自分のアプリケーションを起動すると、私は私の現在の時刻を取得したいと、すべてのn個のインターバルの後、私は非同期タスク

int intv = 15; 
    SimpleDateFormat sd = new SimpleDateFormat(
      "HH:mm:ss"); 
    Date date = new Date(); 
    sd.setTimeZone(TimeZone.getTimeZone("GMT+05:30")); 
    System.out.println(sd.format(date)); 
    String currenttime = sd.format(date); 
    Date myDateTime = null; 
    try 
     { 
     myDateTime = sd.parse(currenttime); 
     } 
    catch (ParseException e) 
     { 
     e.printStackTrace(); 
     } 
    System.out.println("This is the Actual  Date:"+sd.format(myDateTime)); 
    Calendar cal = new GregorianCalendar(); 
    cal.setTime(myDateTime); 

      cal.add(Calendar.MINUTE , intv); //here I am adding Interval 
    System.out.println("This is Hours Added Date:"+sd.format(cal.getTime())); 
    try { 
     Date afterintv = sd.parse(sd.format(cal.getTime())); 
     if(afterintv.after(myDateTime)){ //here i am comparing 
      System.out.println("true.........."); 
      new SendingTask().execute; //this is the function i have to execute 
     } 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

を実行したいしかし、私が何をどのように得ていないのです。

答えて

0

いつかAsyncTaskを実行する場合は、AsyncTaskでThread.sleepを使用できます。この場合、SendingTaskクラスがあります。ここにサンプルがあります:

class SendingTask extends AsyncTask{ 

    // Interval is in milliseconds 
    int interval = 1000; 

    public SendingTask(int interval) { 
     // Setting delay before anything is executed 
     this.interval = interval; 
    } 

    @Override 
    protected Object doInBackground(Object[] params) { 
     // Wait according to interval 
     try { 
      Thread.sleep(interval); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Object o) { 
     super.onPostExecute(o); 
     // update UI and restart asynctask 
     textView3.setText("true.........."); 
     new SendingTask(3000).execute(); 
    } 
} 
+0

** _ありがとうございます** ** –

+0

これはあなたの質問にお答えしますか? –

+0

**はい、実際には動作しています。私はユーザーからの間隔として入力を取らなければならないため、同じ間隔ごとにデータをサーバー**に送信しなければなりませんが、私はなぜ初めて1000と次回3000 15分は15 * 60 * 1000を意味するので、同じ間隔を使用する必要があります –