0

I am using the alarm service for do some task daily at the midnight but today it stop working. I have check and finds that alarm service is not firing up. I am not able to figure out what is the problem.

AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);

        Intent alarmIntent=new Intent(context,UpdateTables.class);

        PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.cancel(pendingIntent);

        Calendar alarmStartTime = Calendar.getInstance();

        alarmStartTime.setTimeInMillis(System.currentTimeMillis());

        //midnight
        alarmStartTime.set(Calendar.HOUR_OF_DAY, 15);
        alarmStartTime.set(Calendar.MINUTE, 12);
        alarmStartTime.set(Calendar.SECOND, 0);


        if(Calendar.getInstance().getTimeInMillis()>alarmStartTime.getTimeInMillis())
        {
            alarmStartTime.add(Calendar.DATE,1);
        }

        Log.i("TIME IS ",Long.toString(alarmStartTime.getTimeInMillis()));


        System.out.println("Updating table time "+alarmStartTime);
        System.out.println("Time in millseconds "+alarmStartTime.getTimeInMillis());


        alarmManager.setInexactRepeating(AlarmManager.RTC,alarmStartTime.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

in the manifest file.

<receiver android:name=".UpdateTables"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>
Ankur Khandelwal
  • 1,042
  • 3
  • 19
  • 40
  • Possible duplicate of [Android: How to use AlarmManager](http://stackoverflow.com/questions/1082437/android-how-to-use-alarmmanager) – Vishal Patoliya ツ Jan 09 '17 at 09:55
  • nops. it is not duplicate @VishalPatoliyaツ . Before today, everything was working but today it stop working. – Ankur Khandelwal Jan 09 '17 at 10:00
  • Did you check in the terminal `adb shell dumpsys alarm` to see if the alarm is gone? And why do you call `cancel()` after declaring the `PendingIntent`? – Nick Friskel Jan 09 '17 at 10:02
  • yup I have run the adb shell dumpsys alarm. it show 16 pending alarm batches but when i search my app package name then not able to find anything. Currently I don't have idea why I have cancel the PendingIntent. Actually i have written this code 3 month so that why specifically why I have cancel the pending intent. @nickfriskel – Ankur Khandelwal Jan 09 '17 at 10:17
  • Ok well your on boot `BroadcastReceiver` is the same receiver that your alarm starts - so if your phone was reboot, it would not restart that alarm but instead start the `UpdateTables` class. Can you post your full code? – Nick Friskel Jan 09 '17 at 10:32
  • added @nickfriskel – Ankur Khandelwal Jan 09 '17 at 10:38
  • hey @nickfriskel thanks I have figured out what is the issue. thanks a lot – Ankur Khandelwal Jan 09 '17 at 10:41

0 Answers0