xamarin 表单 - android 小部件更新
xamarin forms - android widget update
美好的一天,我有一个 xamarin 表单应用程序,我需要在 android 平台上构建一个小部件。
我已经实现了 SimpleWidget
和 HelloAppWidget
的小部件。
问题是:
每次用户单击小部件时,小部件都应更新,显示当前时间。它在 xamarin Android 中更新,但不在表单中更新。在 xamarin 表单中,onUpdate 方法仅在创建小部件后立即使用一次,而不是在用户单击它时调用。
[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 这个实现在 xamarin 表单中工作,我可以在打开小部件时看到时间。但是当我点击它时时间没有更新,就像在 xamarin android
中那样
The widget should be updated everytime user clicks on it, showing the current time.
原因是当你点击你的widget
时,你的widget
class收不到点击事件,不知道什么时候更新时间。
当您点击您的 Widget
时,它会发送一个 broadcast
,您可以覆盖 Widget
中的 OnReceive
以获得此消息,在 OnReceive
方法,您可以更新 Widget
,显示当前时间。像这样的代码:
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);
}
}
我写在Xamarin.Forms项目里,效果像this。
美好的一天,我有一个 xamarin 表单应用程序,我需要在 android 平台上构建一个小部件。
我已经实现了 SimpleWidget
和 HelloAppWidget
的小部件。
问题是:
每次用户单击小部件时,小部件都应更新,显示当前时间。它在 xamarin Android 中更新,但不在表单中更新。在 xamarin 表单中,onUpdate 方法仅在创建小部件后立即使用一次,而不是在用户单击它时调用。
[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 这个实现在 xamarin 表单中工作,我可以在打开小部件时看到时间。但是当我点击它时时间没有更新,就像在 xamarin android
中那样The widget should be updated everytime user clicks on it, showing the current time.
原因是当你点击你的widget
时,你的widget
class收不到点击事件,不知道什么时候更新时间。
当您点击您的 Widget
时,它会发送一个 broadcast
,您可以覆盖 Widget
中的 OnReceive
以获得此消息,在 OnReceive
方法,您可以更新 Widget
,显示当前时间。像这样的代码:
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);
}
}
我写在Xamarin.Forms项目里,效果像this。