0
votes

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

1

1 Answers

2
votes

The widget should be updated everytime user clicks on it, showing the current time.

The reason is that when you click your widget, your widget class cant receive the click event, it didn't know when to update the time.

When you click on your Widget, it will send a broadcast, you could override the OnReceive in your Widget to get this message, in OnReceive method, you could update the Widget, showing the current time. Code like this :

public override void OnReceive(Context context, Intent intent)
{
    if (intent.Action.Equals("android.appwidget.action.APPWIDGET_UPDATE"))
    {
        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 piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
        widgetView.SetOnClickPendingIntent(Resource.Id.widgetBackground, piBackground);

        AppWidgetManager manger = AppWidgetManager.GetInstance(context);
        ComponentName thisName = new ComponentName(context, Java.Lang.Class.FromType(typeof(Widget)).Name);
        manger.UpdateAppWidget(thisName, widgetView);
    }
}

I write in Xamarin.Forms project, effect like this.