3

Basically I've got this Widget, that's supposed to show a Toast once it is clicked.

public class WidgetActivity extends AppWidgetProvider {


 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     for (int i = 0; i < appWidgetIds.length; i++) {  

         int appWidgetId = appWidgetIds[i];  
         Intent intent = new Intent(context, WidgetActivity.class);  
         intent.setAction("ActionOne");  
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);  
         RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
         views.setOnClickPendingIntent(R.id.LinLayWiget, pendingIntent);  
         appWidgetManager.updateAppWidget(appWidgetId, views);  

     }

 }



 @Override  
 public void onReceive(Context context, Intent intent) {  

     Log.e("YYY","YYYY");
     Toast.makeText(context, "AAA", 1500).show();

     if (intent.getAction().equals("ActionOne")) {  

         Log.e("X","X");
         Toast.makeText(context, "I'm CLICKED!", 1500).show();

        }  
     super.onReceive(context, intent);  
 } 




}

manifest:

...
        <receiver android:name="WidgetActivity" android:label="FXMaster" android:icon="@drawable/assiconwi">

            <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widgetprovider" />
                <intent-filter>
                        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
                </intent-filter>
        </receiver>
...

But once the widget is clicked, nothing happens. Any ideas what might be wrong?

Thanks!

Roger Travis
  • 8,402
  • 18
  • 67
  • 94
  • Do you see it get into the OnResume function? Like on debug? If in there do you see a different action? P.S. Probably better to use "ActionOne".equals(intent.getAction()) to avoid possible null pointers :-) – Jackie Feb 11 '12 at 17:11
  • Also I assume that you have 'package="Fart.Widget"' in your AndroidManifest.xml right? Typically it is better to use...new RemoteViews(context.getPackageName(), R.layout.widget_layout); – Jackie Feb 11 '12 at 17:18
  • Thanks for the tips Jackie! I've edited the code, see above, but it still gives no results. :( – Roger Travis Feb 11 '12 at 18:36
  • Perhaps the "LinLayWiget" element can't be clicked? Try adding an image or a button and check result? – span Feb 11 '12 at 19:03

1 Answers1

3

I am here to save you.

1) onUpdate override should call (in the beginning of the method for example)

super.onUpdate(context, appWidgetManager, appWidgetIds);

2) Because of this line:

intent.setAction("ActionOne");

Your intents can be ignored, so the code wonT fall into the if in the onReceive. Add a unique identifier like it is done in this post Android keeps caching my intents Extras, how to declare a pending intent that keeps fresh extras?

On a last note, I d change my class name since it is not an Activity actually.

Cheers.

Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103