Forum Xamarin.Android

Android widget update when app killed

Hi everyone, Is there anyone worked on Xamarin Android widget, I need little help. I need to update my widget for every half an hour, the issue is that whenever my app is killed, update is not working. I tried Alarm Manager with Timer as well like this https://stackoverflow.com/questions/53659897/xamarin-android-widget-with-timer-stops-when-app-killed but couldn't find solution for it. It would be very helpful if anyone have idea on this. Any kind of help will be appreciated. Thanks!

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to create a timer in the custom AppWidgetProvider class to update the widget.

    Check the code:

    [BroadcastReceiver]
    [IntentFilter(new string[] { WidgetConsts.ActionWidgetUpdate })]
    ...
    public class MyWidgetProvider : AppWidgetProvider
    {
        public static System.Timers.Timer widgetTimer = null;
    
        public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
        {
            // Open app on click
            RemoteViews views = new RemoteViews(context.PackageName, Resource.Layout.MyWidget);
    
            Intent launchAppIntent = new Intent(context, typeof(MainActivity));
            PendingIntent launchAppPendingIntent = PendingIntent.GetActivity(context, 0, launchAppIntent, PendingIntentFlags.UpdateCurrent);
            views.SetOnClickPendingIntent(Resource.Id.main, launchAppPendingIntent);
    
            appWidgetManager.UpdateAppWidget(appWidgetIds[0], views);
    
            // set timer for updating the widget views each 5 sec
            if (widgetTimer == null)
            {
                widgetTimer = new System.Timers.Timer();
                widgetTimer.Interval = 5000;
                widgetTimer.Elapsed += OnTimedEvent;
            }
            widgetTimer.Enabled = true;
    
            AlarmManager alarm = (AlarmManager)context.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(context, typeof(AlarmReceiver));
            intent.SetAction(WidgetConsts.ActionWakeup);
            PendingIntent pi = PendingIntent.GetBroadcast(context, 0, intent, PendingIntentFlags.CancelCurrent);
            alarm.SetRepeating(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime(), 1000 * 60, pi);
        }
        public override void OnDisabled(Context context)
        {
            if (widgetTimer != null)
            {
                widgetTimer.Enabled = false;
            }
            else
            base.OnDisabled(context);
        }
    
        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            new Handler(Looper.MainLooper).Post(() =>
            {
                //update the widget periodically
                RemoteViews views = new RemoteViews(Application.Context.PackageName, Resource.Layout.MyWidget);
                AppWidgetManager manager = AppWidgetManager.GetInstance(Application.Context);
                ComponentName thisWidget = new ComponentName(Application.Context, Java.Lang.Class.FromType(typeof(MyWidgetProvider)));
                int[] appWidgetIds = manager.GetAppWidgetIds(thisWidget);
    
                views.SetTextViewText(Resource.Id.myText, "my text");
    
                manager.UpdateAppWidget(appWidgetIds[0], views);
            });
        }
    
        static public void UpdateAppWidget(Context context)
        {
            ...
        }
    }
    

    Refer to: https://stackoverflow.com/a/53870643/11083277

  • MohammedDastagirMohammedDastagir USMember ✭✭

    @Jarvan You shared what I already tried and that same link you shared. Well, thanks for reply.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited April 2

    Try to create a service to keep updating the widget.

    public class CustomService : Service
    {
        private Handler handler;
        private Action runnable;
        private bool isStarted;
        public override void OnCreate()
        {
            base.OnCreate();
    
            handler = new Handler();
    
            runnable = new Action(() =>
            {
                if (isStarted)
                {
                    //start a receiver to update the widget
                }
            });
        }
    }
    

    Check the link:
    https://stackoverflow.com/questions/41652560/android-xamarin-closed-app-notification/41670814#41670814

Sign In or Register to comment.