Widget update at midnight (12 a.m.)

I have a Hijri calendar app with widgets. The Hijri calendar date should change at midnight using the AlarmManager. The problem is that, despite using Alarm Manager to schedule midnight updates, the widget does not update at exactly 12 a.m. It will be updated somewhere between 12 and 1 hours.

Where am I going wrong?

Java:

private static PendingIntent service = null; private static long UPDATES_CHECK_INTERVAL = 24 * 60 * 60 * 1000; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { schedule(context); super.onUpdate(context, appWidgetManager, appWidgetIds); } protected void schedule(Context context) { final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); final Calendar TIME = Calendar.getInstance(); Date now = new Date(); TIME.add(Calendar.DAY_OF_MONTH, 1); TIME.set(Calendar.HOUR_OF_DAY, 0); TIME.set(Calendar.MINUTE, 0); TIME.set(Calendar.SECOND, 0); TIME.set(Calendar.MILLISECOND, 0); long firstTime = (TIME.getTimeInMillis()-now.getTime()); final Intent i = new Intent(context, UpdateService.class); if (service == null) { service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); } m.setRepeating(AlarmManager.RTC, firstTime, UPDATES_CHECK_INTERVAL, service); } 

manifest:

 <receiver android:name="com.example.app.Widget" android:label="Calendar" android:exported="false"> <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="android.appwidget.action.APPWIDGET_ENABLED" /> <action android:name="android.intent.action.TIMEZONE_CHANGED" /> <action android:name="android.intent.action.TIME_SET" /> <action android:name="android.intent.action.DATE_CHANGED" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /> </receiver> 
+4
source share
1 answer

I think you need to change the code related to Calender and AlarmManager.

This calendar represents the exact time of the next day at midnight. and AlarmManager will run the intent at this time every day.

 Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.HOUR, 0); calendar.set(Calendar.AM_PM, Calendar.AM); calendar.add(Calendar.DAY_OF_MONTH, 1); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, service); 

Note. . I think you should add the above code to the onEnabled() method. Here is the highlight of the AppWidgetProvider-related methods .

+4
source

All Articles