0

In my App I have a Sync button that runs a service whenever user wishes to. It saves the date and time when the button is pressed. I want to run that service after every 24 hours of the time it was last sync. For eg. If I pressed the Sync button at "Fri Jan 15 00:00:00 GMT+05:30 2016"(1st date) it should add 24 hours to it to become "Sat Jan 16 00:00:00 GMT+05:30 2016"(2nd date) and then whenever I open any screen of my app, it should check the 2nd date with the current date and if it found that the dates are equal or current date is greater than the 2nd date, it should run that service automatically. I'm getting problem in adding 24 hours to the synced date and comparing it with current date. What should be the solution?

1 Answers1

1

Set alaram of 24 hours when it completes do your stuff. Demo Code:

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
                    Intent intent = new Intent(youractivity.this, ShortTimeEntryReceiver.class);
                    //  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(youractivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    alarmMgr.cancel(pendingIntent);
                    alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 3600 * 24, pendingIntent); 

Create Reciever as:

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


                Toast.makeText(context, "24 hours completed", Toast.LENGTH_LONG).show();


        } catch (Exception e) {
            Log.d("expiredd exception", "exceptionnnn " + e.toString());
        }

    }

Dont forget to register your reciever in the manifest

 <receiver android:name=".ShortTimeEntryReceiver">
            </receiver>
jyomin
  • 1,957
  • 2
  • 11
  • 27