0

I am making a widget on android with xamarin and I use as an example: https://github.com/Nailik/AppWidgetListView/blob/master/AppWidgetListView/WidgetProvider.cs

I adapted well and everything works at launch but I have a problem with the update. I am not using OnUpdate as in the example but I added in my OnReceive because I am using alarm to refresh my widget

so in my OnReceive I did this :

int appWidgetId = intent.GetIntExtra(AppWidgetManager.ExtraAppwidgetId,AppWidgetManager.InvalidAppwidgetId);

Intent svcIntent = new Intent(context, typeof(WidgetServiceList));
svcIntent.SetPackage(context.PackageName);
svcIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, appWidgetId);

svcIntent.SetData(Android.Net.Uri.Parse(svcIntent.ToUri(Android.Content.IntentUriType.AndroidAppScheme)));

updateViews.SetEmptyView(Resource.Id.listViewWidget, Resource.Id.empty_view);

updateViews.SetRemoteAdapter(Resource.Id.listViewWidget, svcIntent);

forcing it works when loading, but no longer when updating due to appWidgetIds [i] (well I think)

would you have a solution to my problem?

edit :

 string miseajourdata = context.GetSharedPreferences("alarme", MODE_PRIVATE).GetString("miseajourdata", "");
        if (miseajourdata == "")
        {
            miseajourdata = "300000";
        }
        long miseajourdataLONG = Long.ParseLong(miseajourdata);

        Intent alarmIntent = new Intent(context, typeof(WidgetAlarmeUpdate));
        var source = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0);
        AlarmManager alarmManager = context.GetSystemService(Context.AlarmService).JavaCast<AlarmManager>();
        alarmManager.SetExact(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime() + miseajourdataLONG, source);

  updateViews.SetTextViewText(Resource.Id.Widgettitre, "Réseau déconnecté !");
  ets....

  ComponentName widgetId = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidgetAlarme)).Name);

                    
                    var svcIntent = new Intent(context, typeof(WidgetServiceList));
                    svcIntent.SetPackage(context.PackageName);
                    svcIntent.PutExtra(AppWidgetManager.ExtraAppwidgetId, widgetId);
                    svcIntent.SetData(Android.Net.Uri.Parse(svcIntent.ToUri(IntentUriType.AndroidAppScheme)));
                    updateViews.SetEmptyView(Resource.Id.listViewWidget, Resource.Id.empty_view);
                    updateViews.SetRemoteAdapter(Resource.Id.listViewWidget, svcIntent);




ComponentName thisWidget = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidgetAlarme)).Name);
        AppWidgetManager manager = AppWidgetManager.GetInstance(context);
        manager.UpdateAppWidget(thisWidget, updateViews);
michael
  • 25
  • 7
  • At first, I can't find the code about updating the appwidget you add in the OnReceive. In the link you give, it updates the appwidget by calling the updateWidgetListView and UpdateAppWidget method. In the resource code, the AppWidgetProvider calling the OnUpdate methed in the OnReceive method to update the appwidget. So you can calling the updateWidgetListView and UpdateAppWidget method in the OnReceive method. – Liyun Zhang - MSFT Dec 20 '21 at 07:53
  • thanks for the information, don't you have a little example? I test but no result. I think I block on the baseline badly for me. – michael Dec 20 '21 at 13:10
  • after looking around all day, I think I have an idea of ​​the problem. it seems that the list cache remains and the update does not appear. change the intention with a value or notifyAppWidgetViewDataChanged. I just need to add a line but how can I change my intention ??? – michael Dec 20 '21 at 19:14
  • Can you show the code about using the AlarmManager? If you want the appwigetprovider update by the onreceive methed itself( not call the onupdate method ),you need to add code in the onreceive method to get the broadcast which the alarmmanager send and then update the appwiget. @michael – Liyun Zhang - MSFT Dec 21 '21 at 06:22
  • I just added the problematic code. updating except for the list. what do you think ? @LiyunZhang-MSFT – michael Dec 21 '21 at 13:13
  • If you use the AlarmManager send a broadcast, you need to add the code about what you want to do in the OnReceieve method. There is an example about using AlarmManager in the AppWidgetProvider in Android.[link](https://stackoverflow.com/questions/7508179/appwidget-alarmmanager-not-updating) – Liyun Zhang - MSFT Dec 22 '21 at 06:16

0 Answers0