Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for AppWidgetProvider's onEnabled & onDisabled is not being called

Since I'm using AlarmManager to perform periodical widget update, I need to ensure onEnabled & onDisabled will work reliably.

However, I realize they will not be triggered sometimes. I'm not the only one who is facing this problem.

Android appWidgetProvider onEnabled never called on tablet

  1. Is there any official bug ticket submitted to Google Android team?
  2. Is there any workaround, especially onDisabled? As I do not want AlarmManager still being triggered repeatably, after the last widget had been removed.

AndroidManifest.xml

    <receiver android:name="org.yccheok.MyAppWidgetProvider"
        android:exported="true" >
        <intent-filter >
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
            <action android:name="android.appwidget.action.APPWIDGET_DELETED" />
            <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_info" />
    </receiver>

public class MyAppWidgetProvider extends AppWidgetProvider {

    private static PendingIntent createAlarmUpdatePendingIntent(Context context) {
        Intent intent = new Intent(context, JStockAppWidgetProvider.class);
        intent.setAction(JStockAppWidgetProvider.ALARM_UPDATE_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    @Override
    public void onEnabled(Context context)
    {        
        super.onEnabled(context);

        PendingIntent pendingIntent = createAlarmUpdatePendingIntent(context);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        int scanSpeed = JStockApplication.instance().getJStockOptions().getScanSpeed();
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + scanSpeed, scanSpeed, pendingIntent);
    }

    @Override
    public void onDisabled(Context context)
    {
        super.onDisabled(context);

        PendingIntent pendingIntent = createAlarmUpdatePendingIntent(context);
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }
like image 509
Cheok Yan Cheng Avatar asked Oct 20 '22 12:10

Cheok Yan Cheng


1 Answers

you can check in method a number of instances that are currently running.

private boolean hasInstances(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
            new ComponentName(context, this.getClass()));
    return (appWidgetIds.length > 0);
}
like image 124
Autocrab Avatar answered Oct 23 '22 02:10

Autocrab