I have a widget that works fine on Android 9. Recently, I got a new phone and it runs Android 12. I was testing my widget and for some reason, it does not update automatically after 30 mins. The widget loads fine, when I add it to the screen, but after that - nothing happens. It is a weather widget which should update every 30 mins (as it does on Android 9 device).
Any advice is appreciated.
I am using AlarmManager as follows:
private PendingIntent createClockTickIntent(Context context) {
Intent alarm = new Intent(context, StackWidgetProvider.class);
alarm.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); // Set appwidget update action
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidgetComponentName = new ComponentName(context.getPackageName(),getClass().getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName);
alarm.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getActivity(context,
0, alarm, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(context,
0, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
}
return pendingIntent;
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
System.err.println("OnEnabled");
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1800000, createClockTickIntent(context));
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 1000*1800, createClockTickIntent(context));
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
System.err.println("Received intent " + intent);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(createClockTickIntent(context));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 1000*1800, createClockTickIntent(context));
}
I also have the following in the manifest...
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />