JustCurious
JustCurious

Reputation: 1858

Android Widget onClick

I want to be able to click on a widget and launch a dialog box. I have read the official documentation as some of the unofficial ones. I initially wanted to launch a new activity but even this fails. I get the following in Logcat but I cant really see anything.

11-14 21:28:47.929: INFO/ActivityManager(116): Starting: Intent { flg=0x10000000 cmp=com.android.app/.Execute bnds=[179,89][300,160] } from pid -1

I guess the above means that the intent was passed... But the activity was actually not started. Should the activity to be started be a normal one?

The code used is:

public class ExampleAppWidgetProvider extends AppWidgetProvider {

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        // Create an Intent to launch ExampleActivity
        Intent intent = new Intent(context, ExampleActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        // Get the layout for the App Widget and attach an on-click listener
        // to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
        views.setOnClickPendingIntent(R.id.button, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
}

Any thoughts?

Upvotes: 3

Views: 4748

Answers (1)

Mark Allison
Mark Allison

Reputation: 21909

The most likely cause of the problem would be that you haven't declared ExampleActivity in your Manifest.

<UPDATE>

You could also try using a unique number for argument 2 of your PendingIntent creation and also put a sensible flag in argument 4:

    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT );

Upvotes: 2

Related Questions