2017-08-17 6 views
0

私のアプリでは、ユーザーが30日間更新しなければオブジェクトを更新する必要があります。私はこれに近づくためにいくつかのことを試してみましたが、それらのどれも私の最初の試みは、単にオブジェクトが更新された時刻を格納し、現在の時刻にそれを比較することであったSystem.currentTimeMillies()Android:OS時間に依存しない一定の時間からのカウント日数

を使用して

1.働いていません。 AlarmManager

を使用して

2. ...すべてがうまく働いたが、問題は、ユーザーがOSの時間を変更することができること、した後、時間のチェックは役に立たないだろうここで私は、上記と同様の問題を抱えていましたここでは、タイマー

が含まれているサービスを実装する

3.私はちょうど30日までカウントアップタイマーでServiceを実装しました。これは最善の解決策に見えましたが、私がアプリケーションを閉じるとサービスが停止します。私のサービスの onCreateonStartCommandこの(私はテストのために2分に、30日に変更し、それが複数のオブジェクトに対して複数のタイマーを含んでいる)のようになります。また、私は共有でタイマーマップを保存しようとした

@Override 
public void onCreate() { 
    Log.i(TAG, "[onCreate]"); 
    super.onCreate(); 
    registerReceiver(new StopServiceReceiver(), new IntentFilter(STOP_SERVICE_REQUEST)); 
    context = this; 

    //retrieveTimers(); 
} 


@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if (intent == null) { 
     Log.i(TAG, "[onStartCommand] intent = null"); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    //prevent starting 2 timers for the same id 
    String id = intent.getStringExtra(KEY_CARD_ID); 
    if (timerHashMap.containsKey(id)) { 
     Log.i(TAG, "[onStartCommand] timer already exists"); 
     return super.onStartCommand(intent, flags, startId); 
    } 

    //create and start timer 
    Log.i(TAG, "[onStartCommand] schedule timer"); 
    Timer timer = new Timer(); 
    timer.schedule(new CounterTask(id), MAX_MILLIES_WITHOUT_UPDATE); 
    timerHashMap.put(id, timer); 
    // storeTimers(); 

    return super.onStartCommand(intent, flags, startId); 
} 

タイマーを続ける必要があり、それを保存するタイマーを連番にする必要があったので、私は気づいた。

ありがとう!

答えて

1

JobScheduler(またはそのバリエーション)を使用できます。

30日後にジョブを実行するようにスケジュールするオプションは、Minimum LatencyPeriodicです。 JobIntentServiceは、下位互換性のために最新のサポートライブラリとFirebase JobDispatcherでありScheduling jobs like a pro with JobScheduler

は、詳細はこちらの記事を参照してください。

関連する問題