Android widget stops working after a while?

心不动则不痛 提交于 2019-11-30 19:49:51

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!