I've a widget on my app and Service in order to update automatically.
Widget has a ImageButton which triggers updating content manually. But I couldn't handle click event.
Here is my AppWidgetProvider:
public class MyWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews rmViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent active = new Intent(context, MyWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button 1");
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, active, 0);
rmViews.setOnClickPendingIntent(R.id.buttonus1, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, rmViews );
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
Log.d(debug_tag, intent.getAction());
}
}
}
When I clicked the button nothing changed. It doesn't even logged.
Manifest.xml declaration:
<receiver android:name=".MyWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.app.example.MyWidget.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget"/>
</receiver>
What's wrong?
By the way, if I could handle the click, how should I use for updating manually? Do I pass it to service and which way?
Edit: Changed getActivity() with getBroadcast() and handled click well. But couldn't pass this message to Service. Any recommendations?