1

So far i have coded:

public class WidgetActivity extends AppWidgetProvider
{    
    public void onReceive(Context context, Intent intent)
    {


        String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
        {

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

            Intent settingsIntent = new Intent(context, Info.class);
            PendingIntent clickPendIntent = PendingIntent.getActivity
                    (context, 0, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.Widget, clickPendIntent);





            AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);


        }
    }
}

To no success i click the widget when its on screen and nothing launches. Am I missing anything?

MarchingHome
  • 1,184
  • 9
  • 15

1 Answers1

1

Put the code that makes sure the PendingIntent is set, in the onUpdate() method instead. This makes sure that as soon as the widget is put on the homescreen that the PendingIntent is set.

So:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgets) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_activity);
    Intent intent = new Intent(context, Info.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.Widget, pendingIntent);

Also:

If you would also like to have the info Activity pop up automatically when first adding the widget to the home screen, you should read this userful piece of information.

MarchingHome
  • 1,184
  • 9
  • 15
  • 1
    I don't think you need the rest of the code to solve the problem of an `Activity` not launching when pressing the widget. – MarchingHome Oct 18 '12 at 00:39
  • is that ok? should i leave the update period millis at 0? – Adrian Orta Oct 18 '12 at 00:49
  • 1
    That is possible. That would not cause any problems for this specific thing (pressing the widget to start the `Activity` defined in the `Intent`). The first onUpdate (when putting the widget on the homescreen) will still be received. – MarchingHome Oct 18 '12 at 00:55
  • ive added android:configure=".Info" to the widget provider manifest but now when i try to add the widget to the homescreen it gives a Toast that says the app isnt installed. – Adrian Orta Oct 18 '12 at 01:10
  • 1
    OK, I'm learning here as we go. Please use the full packagename with the class then. Example: `com.example.yourappname.Info`. And let me know if it works then. A full reinstall of the app is recommended (deleting data and cache and deleting the app manually via settings --> applications) before installing it again. – MarchingHome Oct 18 '12 at 01:13
  • 1
    Yup that launches it. Thanks so much – Adrian Orta Oct 18 '12 at 01:15
  • I was successful in launching the activity after placing the widget but now my widget is not on the home screen when i press home or back. Is there any possible reason for this? – Adrian Orta Oct 18 '12 at 14:04
  • No, I placed the widget on the homescreen which automatically launches the activity so i never see the widget actually placed. Then when i press home or back i realize the widget was never placed. Pretty much i drag and drop the widget to the homescreen, it launches the activity, and thats it. But i want the widget to stick – Adrian Orta Oct 18 '12 at 14:15
  • is there other file i might need to show you to solve the issue? – Adrian Orta Oct 18 '12 at 14:27
  • 1
    I understand now, after reading this: [Android developer explanation](http://developer.android.com/guide/topics/appwidgets/index.html#Configuring). The `Info` activity that you launch should return a result. Please read it carefully and it will help you. Note that this all is only necessary for it to launch automatically when placed on the home screen. For opening an activity by pressing the widget, this is not necessary and the `onUpdate` part of my code will suffice. – MarchingHome Oct 18 '12 at 15:17
  • 1
    That was just the code i needed. I always looked there for an answer but couldnt really see how to use it till now. Is there any way to make it so that when i press the button to update the widget it changes an image thats a part of the widget? kind of like a theme chooser – Adrian Orta Oct 18 '12 at 16:23
  • 1
    There is such a way, yes. But I have never implemented it. If you can't seem to find it out, please make it a new question :) – MarchingHome Oct 18 '12 at 16:39
  • Ok, asked. :) under Changeable Widget Dial if you wanna give it a look. thanks for all the help. – Adrian Orta Oct 18 '12 at 17:23