2016-11-19 21 views
0

毎日午前6時にジョブサービスを毎日実行して仕事をしたい。私が設定できるのはsetPeriodなので、毎日午前6時にタスクを実行または再スケジュールする方法を見つけることができませんでした。前もって感謝します。スケジュール毎日午前6時にPeriodicTask Android

private static long periodSecs = 7200L; //TODO: Set 6AM everyday 
    private static final String JOB_TAG = "NOTIFICATION_JOB"; 

    private void scheduleJob() { 
     Timber.i("scheduleJob"); 
     Task task = new PeriodicTask.Builder() 
       .setService(Job.class) 
       .setPeriod(periodSecs) 
       .setTag(JOB_TAG) 
       .setRequiredNetwork(Task.NETWORK_STATE_CONNECTED) 
       .setPersisted(true) 
       .setUpdateCurrent(true) 
       .build(); 

     GcmNetworkManager.getInstance(this).schedule(task); 
    } 
+0

あなたはアラームマネージャまたはジョブスケジューラを使用して検討していますか? – HaroldSer

答えて

0

AlaraManagerは定期的にタスクを実行します。 このように使用してください。

public void scheduleAlarm(View v) 
    { 
     //set time here 
     Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000; 

     // Create an Intent and set the class that will execute when the Alarm triggers.   
     Intent intentAlarm = new Intent(this, AlarmReceiver.class); 

     // onReceive() method of this class will execute when the broadcast from your alarm is received. 

     // Get the Alarm Service 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     // Set the alarm for a particular time. 
     alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT)); 
     Toast.makeText(this, "Alarm Scheduled for Tommrrow", Toast.LENGTH_LONG).show();  
    } 

AlarmReceiverクラス

public class AlarmReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     //perform what ever the operation you want 
     //when alarm triggers daily on your time 

    } 
} 
+0

でも、これはTimerTaskを使って行うことができますが、一部のデバイスでは正しく動作せず、スペースが必要な場合はシステムによってしばらくしてから終了します。 – Radhey

関連する問題