3

I have developed a simple flashlight with widgets. But after a certain time, for some strange reason, the widget does not respond, add a new - everything is OK, and the old should be deleted.

public class XFlashLightAppWidgetProvider extends AppWidgetProvider {

/** Action name for updating widget receiver */
private static final String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    // Loop through all widgets of this application
    for (int i = 0; i < appWidgetIds.length; i++) {
        // Creating intent to send to the widget broadcast receiver
        // with update action
        Intent intent = new Intent(context,
                XFlashLightAppWidgetProvider.class);
        intent.setAction(ACTION_WIDGET_RECEIVER);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.appwidget);

        // When user click on the widget - send broadcast intent to the
        // widget
        // broadcast receiver for enabling or disabling flashlight and
        // updating widgets
        views.setOnClickPendingIntent(R.id.widgetLampImage, pendingIntent);

        // When user added new widget on his device - checking flashlight
        // and if flashlight is enabled - changing widget icon
        if (CameraHelper.isTorchFlashMode()) {
            views.setImageViewResource(R.id.widgetLampImage,
                    R.drawable.widget_on);
        }

        // Update each widget of the application
        appWidgetManager.updateAppWidget(appWidgetIds[i], views);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    // Check whether the device supports flash on the camera and the
    // receiver
    // received correct action
    if (CameraHelper.checkCameraFlashLightHadrware(context)
            && (action.equals(ACTION_WIDGET_RECEIVER)
                    || action.equals(XFlashLightActivity.ACTION_WIDGET_ON) || action
                        .equals(XFlashLightActivity.ACTION_WIDGET_OFF))) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.appwidget);

        // Checking action name, if action name equals
        // "ActionReceiverWidget" -
        // enable or disable flashlight and update widget icon, else if
        // action name
        // equals "ActionReceiverOn" or "ActionReceiverOff" - change widget
        // icon only
        if (action.equals(ACTION_WIDGET_RECEIVER)) {
            if (!CameraHelper.isTorchFlashMode()) {
                CameraHelper.initializeCameraFlash();
                views.setImageViewResource(R.id.widgetLampImage,
                        R.drawable.widget_on);
            } else {
                CameraHelper.releaseCamera();
                views.setImageViewResource(R.id.widgetLampImage,
                        R.drawable.widget_off);
            }
        } else if (action.equals(XFlashLightActivity.ACTION_WIDGET_ON)) {
            views.setImageViewResource(R.id.widgetLampImage,
                    R.drawable.widget_on);
        } else if (action.equals(XFlashLightActivity.ACTION_WIDGET_OFF)) {
            views.setImageViewResource(R.id.widgetLampImage,
                    R.drawable.widget_off);
        }

        // Update each widget of the application
        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        int[] appWidgetIds = appWidgetManager
                .getAppWidgetIds(new ComponentName(context,
                        XFlashLightAppWidgetProvider.class.getName()));
        for (int i = 0; i < appWidgetIds.length; i++) {
            appWidgetManager.updateAppWidget(appWidgetIds[i], views);
        }
    }
    super.onReceive(context, intent);
  }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

1

Android will stop your home widget when it faces a low memory situation. Then it will restart your home widget after cleaning the RAM automatically. But this time, your home widget will get a different pid from the last one, so it cannot respond the broadcast.

I think you should check whether your app has memory leak. And also you can try a different implement by using alarmManager. Move all your onUpdate() into a alarmReceiver.

See this one. Updating app widget using AlarmManager

Or try a update service.

Community
  • 1
  • 1
Zhenghong Wang
  • 2,117
  • 17
  • 19
0

Thanks to Zhenghong Wang. Instead, onReceive method in AppWidgetProvider I have used the service

public class WidgetUpdateService extends Service {

    @Override
    public void onStart(Intent intent, int startId) {
        Context context = getApplicationContext();
        String action = intent.getAction();

        if (CameraHelper.checkCameraFlashLightHadrware(context)
            && (action.equals(XFlashLightAppWidgetProvider.ACTION_WIDGET_RECEIVER)
                    || action.equals(XFlashLightActivity.ACTION_WIDGET_ON) || action
                        .equals(XFlashLightActivity.ACTION_WIDGET_OFF))) {
            RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.appwidget);

            if (action.equals(XFlashLightAppWidgetProvider.ACTION_WIDGET_RECEIVER)) {
                if (!CameraHelper.isTorchFlashMode()) {
                    CameraHelper.initializeCameraFlash();
                    views.setImageViewResource(R.id.widgetLampImage,
                        R.drawable.widget_on);
                } else {
                     CameraHelper.releaseCamera();
                    views.setImageViewResource(R.id.widgetLampImage,
                        R.drawable.widget_off);
                }
            } else if (action.equals(XFlashLightActivity.ACTION_WIDGET_ON)) {
                views.setImageViewResource(R.id.widgetLampImage,
                    R.drawable.widget_on);
            } else if (action.equals(XFlashLightActivity.ACTION_WIDGET_OFF)) {
                views.setImageViewResource(R.id.widgetLampImage,
                    R.drawable.widget_off);
            }

            AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
            int[] appWidgetIds = appWidgetManager
                .getAppWidgetIds(new ComponentName(context,
                        XFlashLightAppWidgetProvider.class.getName()));
            for (int i = 0; i < appWidgetIds.length; i++) {
                appWidgetManager.updateAppWidget(appWidgetIds[i], views);
            }
        }
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
Community
  • 1
  • 1