I am currently creating my first widget with three ImageButtons
.
I've followed the answer described by Clickable widgets in android pretty closely, however I fail to get it working.
I've defined three strings which are my actions:
private String playAction = "playService";
private String stopAction = "stopService";
private String resetAction = "resetService";
Next in my OnUpdate()
function I add the setOnclickPendingIntent
:
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setOnClickPendingIntent(R.id.widget_play, getPendingSelfIntent(context, playAction));
remoteViews.setOnClickPendingIntent(R.id.widget_pause, getPendingSelfIntent(context, stopAction));
remoteViews.setOnClickPendingIntent(R.id.widget_reset, getPendingSelfIntent(context, resetAction));
manager = appWidgetManager;
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
// Update the widgets via the service
context.startService(intent);
}
The getPendingSelfIntent
function is defined as followed:
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
However the onReceive
function receives none of the above defined actions. I currently log all the actions of incoming intents, but the only one that passes is the android.appwidget.action.APPWIDGET_UPDATE
action.
For the sake of completion, this is how I've defined the actions in my Manifest:
<receiver
android:name="MyWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="My Widget">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="playService" />
<action android:name="stopService" />
<action android:name="resetService" />
</intent-filter>
</receiver>
I've read in the Android docs that elements that are part of a collection (ListView
etc.) cannot be set using the setOnClickPendingIntent
function. I have my three buttons in my layout. To make sure this is not causing the error, here's what the XML looks like:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/widget_play"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginRight="5dp"
android:src="@drawable/run"/>
<ImageButton
android:id="@+id/widget_pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_marginRight="5dp"
android:src="@drawable/pause"/>
<ImageButton
android:id="@+id/widget_reset"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/reset"/>
</LinearLayout>
Any help on this matter is appreciated, as I am all out of ideas. I've searched SO for similar problems yet they all describe what I've already tried.