2

I have this widget in android, that displays an image and gets updated periodically by a broadcast receiver. Now I want to give the user the ability to start a configuration screen (preference activity) by clicking on the widget. Here it goes:

The widget provider class:

public class MyProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

        for (int widgetId : appWidgetIds) {
            // setting onClickListener - at least i TRY to
            Intent onClickIntent = new Intent(context, Preferences.class);

            PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);
            rViews.setOnClickPendingIntent(R.id.graph, onClickPendingIntent);

            appWidgetManager.updateAppWidget(widgetId, rViews);
        }
    }
}

The updates of the widget are triggered from here:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());

        int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
        ComponentName myWidget = new ComponentName(context.getApplicationContext(), MyProvider.class);
        int[] allMyWidgetIds = appWidgetManager.getAppWidgetIds(myWidget);  

        for (int widgetId : allMyWidgetIds)
        {
            RemoteViews rViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            // update the widgets content, i.e. picture
            appWidgetManager.updateAppWidget(widgetId, rViews);
        }

        // schedule next update
        Calendar c = Calendar.getInstance();
        c.add(Calendar.SECOND, Config.UPDATE_RATE);
        Date d = new Date(c.getTimeInMillis());

        Intent timerIntent = new Intent(context.getApplicationContext(), TimeIntervalReceiver.class);

        timerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
        PendingIntent timerPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, timerIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager aManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC, d.getTime(), timerPendingIntent);
    }
}

Just to mention it: the Preference.java-Activity is registered in the Manifest and can be started correctly (I verified this by setting it as the startup activity). Anyhow, the onclick-functionality does not work at all :( I also tried to move the code to the "MyReceiver"-class which updates the widgets: nothing :(

I already looked at: * Android: Clickable imageview widget * http://www.vogella.de/articles/AndroidWidgets/article.html

However, this didn't help at all :(

anyone here who can tell me where my problem is?

Community
  • 1
  • 1
xenonite
  • 1,671
  • 4
  • 28
  • 43

2 Answers2

3

Try changing this line:

PendingIntent onClickPendingIntent = PendingIntent.getBroadcast(context, 0, onClickIntent, 0);

to

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
Woodsy
  • 3,177
  • 2
  • 26
  • 50
0

PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);

Retrieve a PendingIntent that will start a new activity

http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)

Plz refer link for full reference