Android widget stops working after a while?

后端 未结 2 616
清酒与你
清酒与你 2021-01-05 08:29

I have a flashlight app with a widget. The widget is used to turn the flashlight on and off and does not display main activity or anything. After a few hours, however, the w

相关标签:
2条回答
  • 2021-01-05 09:23

    Alright guys, I finally got time to fix this problem once and for all :)

    I created more methods for the provider instead of doing everything in onUpdate, one important method needed:

        public static PendingIntent buildButtonPendingIntent(Context context) {
          Intent intent = new Intent();
          intent.setAction("COM_FLASHLIGHT");
          return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        }

    And this method is called through the receiver when the widget is clicked using the code below:

    private void turnFlash(Context context) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_layout);
        views.setOnClickPendingIntent(R.id.imageButton,       WidgetProvider.buildButtonPendingIntent(context));
    }

    That is all, no more hiccups!

    0 讨论(0)
  • 2021-01-05 09:25

    Look for this post, I think this problem is explained here the dark side of app widgets

    0 讨论(0)
提交回复
热议问题