3

I have a widget I'm trying to build that consists of only a button. What I'm wanting to do is have the button clicked and then run some simple piece of code (in my test it's a toast alert). It seems to work fine initially, but suddenly the button stops responding to clicks. I've noticed it consistently after the phone has been asleep. Here is my code for the AppWidgetProvider.

onUpdate:

for (int appWidgetId : appWidgetIds) {
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.my_widget);
    Intent intent = new Intent(context, MyNewWidgetProvider.class);
    intent.setAction("MyCode");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    remoteView.setOnClickPendingIntent(R.id.my_btn, pendingIntent);

    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteView);
}

onReceive:

super.onReceive(context, intent);

if (intent.getAction().equals("MyCode")) {          
    Toast toast = Toast.makeText(context, "It worked", Toast.LENGTH_SHORT);
    toast.show();   

}

I'm rather stumped, so if anyone can help point me in the right direction I would appreciate it. Like I said, it works fine until the phone is asleep for a minute or two, then it completely stops responding to clicks.

Thank You!

Brian
  • 31
  • 1
  • 2

1 Answers1

1

The pending intent is "burned" after each use. You need to set it again. Or wait for the widget to get refreshed, then it happens, too, but that's probably not the desired way.

Mark
  • 520
  • 1
  • 7
  • 21
  • If that was the case, the widget would have worked only once. The pending intent seems to get burned only after the phone has been asleep for some time. – faizal Jul 16 '14 at 06:04
  • Hi @Mark, Do you know any solution to this problem? I'm keep on searching, but couldn't find any solution. Also I'm new to android, and working on cross-platform mobile apps. – Muhammad Mehdi Raza Feb 13 '17 at 10:11
  • @MuhammadMehdiRaza not working on it, no. If I remember correctly (that was almost 6 years ago) it did work when you set the intent again after it was triggered/burnt. – Mark Feb 14 '17 at 10:59