Good day, i have a xamarin forms app, and i need to build a widget on an android platform.
I've implemented a widget from SimpleWidget
and HelloAppWidget
.
The problem is:
The widget should be updated everytime user clicks on it, showing the current time. And its updating in xamarin Android , but not in the Forms. In xamarin forms, onUpdate method is using only once right after creating the widget, and not calling when user clicks on it.
[BroadcastReceiver(Label = "WIIDget")]
[IntentFilter(new string[] {"android.appwidget.action.APPWIDGET_UPDATE"})]
[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class Widget : AppWidgetProvider
{
public static string AnnouncmentClick = "AnnouncementClickTag";
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(Widget)).Name);
appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);
widgetView.SetTextViewText(Resource.Id.widgetMedium, "I am a title");
widgetView.SetTextViewText(Resource.Id.widgetSmall, $"Last click in {DateTime.Now}");
var intent = new Intent(context, typeof(Widget));
intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
intent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetIds);
var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
widgetView.SetOnClickPendingIntent(Resource.Id.widgetBackground, piBackground);
return widgetView;
}
}
P.S this impementation is working in xamarin forms, and I can see the time when I open the widget. but the time is not updating when I click on it, as it does in xamarin android