0

I set a dot on my widget with the number of notifications. I have such a problem that after about 40-60 seconds the number of notifications is updated. Is this a widget refresh problem? Is there any way to speed up this process? I used this tutorial: enter link In appwidgetprovider.xml I set android: updatePeriodMillis = "0" My code:

[BroadcastReceiver(Label = "MobileApp")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
// The "Resource" file has to be all in lower caps
[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class AppWidget : AppWidgetProvider
{
    private static string IconClick = "AnnouncementClickTag";
    public MobileNotificationsService MobileNotificationsService = new MobileNotificationsService();
    /// <summary>
    /// This method is called when the 'updatePeriodMillis' from the AppwidgetProvider passes,
    /// or the user manually refreshes/resizes.
    /// </summary>
    public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name);
        appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));


    }
    public override void OnEnabled(Context context)
    {

        AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);
        Intent ai = new Intent(context, typeof(AppWidget));
        ai.SetAction("android.appwidget.action.APPWIDGET_UPDATE");
        PendingIntent pi = PendingIntent.GetBroadcast(context, 0, ai, PendingIntentFlags.UpdateCurrent);
        am.SetRepeating(AlarmType.ElapsedRealtime, 10, 1000 * 1, pi);

    }
    private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
    {
        // Retrieve the widget layout. This is a RemoteViews, so we can't use 'FindViewById'
        var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);

        SetTextViewText(widgetView);
        RegisterClicks(context, appWidgetIds, widgetView);

        return widgetView;
    }

    public void SetTextViewText(RemoteViews widgetView)
    {
        var notifications = App.Current.Properties["ID"].ToString();
        if (int.Parse(notifications) > 0)
        {
            widgetView.SetViewVisibility(Resource.Id.badge_notification_1, ViewStates.Visible);
            widgetView.SetTextViewText(Resource.Id.badge_notification_1, $"{notifications}");
        }
        else
        {
            widgetView.SetViewVisibility(Resource.Id.badge_notification_1, ViewStates.Invisible);
        }
        
        //widgetView.SetTextViewText(Resource.Id.widgetSmall, string.Format("Last update: {0:H:mm:ss}", DateTime.Now));
    }

    private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
    {
        var intent = new Intent(context, typeof(AppWidget));
        intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
        intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);

        // Register click event for the Background
        var piBackground = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.UpdateCurrent);
        //widgetView.SetOnClickPendingIntent(Resource.Id.badge_notification_1, piBackground);


            // Register click event for the Icon-icon
            widgetView.SetOnClickPendingIntent(Resource.Id.widgetIcon, GetPendingSelfIntent(context, IconClick));
    }

    private PendingIntent GetPendingSelfIntent(Context context, string action)
    {
        var intent = new Intent(context, typeof(AppWidget));
        intent.SetAction(action);
        return PendingIntent.GetBroadcast(context, 0, intent, 0);

    }

    /// <summary>
    /// This method is called when clicks are registered.
    /// </summary>
    public override void OnReceive(Context context, Intent intent)
    {
        base.OnReceive(context, intent);

        // Check if the click is from the "Announcement" button
        if (IconClick.Equals(intent.Action))
        {
            var pm = context.PackageManager;
            try
            {
                var packageName = "app.mobile";
                var launchIntent = pm.GetLaunchIntentForPackage(packageName);
                context.StartActivity(launchIntent);
            }
            catch
            {
                // Something went wrong :)
            }
        }
    }
}
  • As official document mentioned, the AppWidget manager may place a limit on how often a AppWidget is updated. And updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes. The OnUpdate menthod is called when the `updatePeriodMillis` from the AppwidgetProvider passes, or the user manually refreshes/resizes. – Wendy Zang - MSFT Oct 26 '21 at 07:16
  • How can I manually refresh? – Kosciuszkobest123 Oct 26 '21 at 12:37
  • You could triger via buttom click to call onupdate method. https://stackoverflow.com/questions/49028646/how-to-call-onupdate-method-of-widget-on-button-click https://stackoverflow.com/questions/40776920/how-to-manually-call-onupdate-from-within-the-widget-itself – Wendy Zang - MSFT Oct 27 '21 at 07:42

0 Answers0