question

Forsaken-7151 avatar image
0 Votes"
Forsaken-7151 asked ·

Xamarin.Android Widget not working when app is closed

I have made a Xamarin.Android Widget with a Button and TextView.

Currently, the widget does not update the TextView when the Button is pressed when the app is not open. However, if the app is in the background, then pressing the Button will update the TextView.

 public class WidgetClass : AppWidgetProvider, IUpdateDataService
 {
     public static String SaveClick = "Save Product";
    
     public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
     {
         var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
         appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds));
     }
    
     private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
     {
         var widgetView = new RemoteViews(context.PackageName, Resource.Layout.widget);
         UpdateData(widgetView);
         RegisterClicks(context, appWidgetIds, widgetView);
         return widgetView;
     }
    
     private void UpdateData(RemoteViews widgetView)
     {
         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
         widgetView.SetTextViewText(Resource.Id.textView1, "Last Product: " + App.latestProduct.ProductSaveTime.ToString("g", currentCulture));
     }
    
     private void RegisterClicks(Context context, int[] appWidgetIds, RemoteViews widgetView)
     {
         var intent = new Intent(context, typeof(WidgetClass));
         intent.SetAction(AppWidgetManager.ActionAppwidgetUpdate);
         intent.PutExtra(AppWidgetManager.ExtraAppwidgetIds, appWidgetIds);
    
         widgetView.SetOnClickPendingIntent(Resource.Id.buttonSave, GetPendingSelfIntent(context, SaveClick));
     }
    
     private PendingIntent GetPendingSelfIntent(Context context, string action)
     {
         var intent = new Intent(context, typeof(WidgetClass));
         intent.SetAction(action);
         return PendingIntent.GetBroadcast(context, 0, intent, 0);
     }
    
     public override void OnReceive(Context context, Intent intent)
     {
         base.OnReceive(context, intent);
    
         if (SaveClick.Equals(intent.Action))
         {
               Product product = new Product
               {           
                   ProductSaveTime = DateTime.Now       
               }; 
    
             App.latestProduct = product; 
    
             RemoteViews remoteViews = new RemoteViews(context.PackageName, Resource.Layout.widget);
             UpdateData(remoteViews);
             AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(context);
             ComponentName componentName = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
             appWidgetManager.UpdateAppWidget(componentName, remoteViews);
         }
    }
    
    public void UpdateWidgetUI()
     {
         var widgetView = new RemoteViews(MainActivity.Instance.PackageName, Resource.Layout.widget);
         UpdateData(widgetView);
    
         AppWidgetManager appWidgetManager = AppWidgetManager.GetInstance(MainActivity.Instance);
         ComponentName componentName = new ComponentName(MainActivity.Instance, Java.Lang.Class.FromType(typeof(WidgetClass)).Name);
         appWidgetManager.UpdateAppWidget(componentName, widgetView);
     }
 }

App.cs:

 public static Product latestProduct;

Is it possible to make the Button function when the app is not running?


dotnet-csharpdotnet-xamarinformsdotnet-android
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

Is it possible to make the Button function when the app is not running

Starting in Android 8.0 (API level 26), an Android application no longer have the ability to run freely in the background. Try using the foreground sevice by starting a foregroud notification to keep the widget working even when it is closed.

[Service(Enabled = true)]
public class MyService : Service
{
    private Handler handler;
    private Action runnable;
    private bool isStarted;
    ...
    public override void OnCreate()
    {
        base.OnCreate();

        handler = new Handler();
        //here is what you want to do always, i just want to push a notification every 5 seconds here
        runnable = new Action(() =>
        {
            if (isStarted)
            {
                DispatchNotification();
                handler.PostDelayed(runnable, 5000);
            }
        });
    }
    ...
}

Similar issue: https://stackoverflow.com/questions/60028587/background-service-with-isolated-process-in-xamarin-forms/60032046

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.