Skip to content

Commit

Permalink
Added start widget alarm on reboot and cancel when deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
caleb-yun committed Jul 18, 2018
1 parent 091b051 commit 09ae7d5
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions app/src/main/java/layout/OverWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
Expand Down Expand Up @@ -31,26 +32,25 @@ public class OverWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
//int interval = prefs.getInterval();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, OverWidgetProvider.class);
intent.setAction(OverWidgetProvider.REFRESH_INTENT);
intent.putExtra("appWidgetId", appWidgetId);
PendingIntent pi = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

/*int updateInterval = 60*60*1000;
int updateInterval = 0;
Profile profile = WidgetUtils.loadUserPrefOffline(context, appWidgetId);
if (profile != null)
updateInterval = profile.getUpdateInterval();
Log.d(TAG, "(" + appWidgetId + ") Update Interval: " + updateInterval);

PendingIntent pi = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), updateInterval, pi);*/
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), updateInterval, pi);
//alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), 10*1000, pi);

context.sendBroadcast(intent);

context.sendBroadcast(intent);
WidgetUtils.setLoadingLayout(context, appWidgetId, AppWidgetManager.getInstance(context));
}
}
Expand All @@ -68,19 +68,26 @@ public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) {
WidgetUtils.deletePrefs(context, appWidgetId);

// Cancel alarm
Intent intent = new Intent(context, OverWidgetProvider.class);
intent.setAction(OverWidgetProvider.REFRESH_INTENT);
intent.putExtra("appWidgetId", appWidgetId);
PendingIntent pi = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
assert alarmManager != null;
alarmManager.cancel(pi);
}
}

@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
//AlarmUtil.scheduleUpdate(context);
}

@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
//AlarmUtil.clearUpdate(context);
}

@Override
Expand Down

0 comments on commit 09ae7d5

Please sign in to comment.