我正在开发一个简单的C#Android 应用程序,它在后台运行并执行特定计算,我还有一个带有 TexTview 的小部件,我想在其中显示计算结果。
服务.cs
Intent intent = new Intent("TEST");
intent.PutExtra("alltotale", total.ToString());
this.SendBroadcast(intent);
然后在BroadcastReceiver课堂上,我有:
接收器.cs
public override void OnReceive(Context context, Intent intent)
{
string nh = intent.GetStringExtra("alltotale");
Toast.MakeText(context, nh, ToastLength.Short).Show();
}
现在我想重新使用这个值并将它显示在小部件 TextView 中,但它不起作用,我的Widget.
AppWidget.cs
[BroadcastReceiver(Label = "Widget1")]
[IntentFilter(new string[] { "android.appwidget.action.APPWIDGET_UPDATE" })]
[MetaData("android.appwidget.provider", Resource = "@xml/appwidgetprovider")]
public class AppWidget : AppWidgetProvider
{
Intent intent;
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));
}
private RemoteViews BuildRemoteViews(Context context, int[] appWidgetIds)
{
var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);
SetTextViewText(widgetView);
RegisterClicks(context, appWidgetIds, widgetView);
return widgetView;
}
private void SetTextViewText(RemoteViews widgetView)
{
string oo = intent.GetStringExtra("alltotale");
widgetView.SetTextViewText(Resource.Id.widgetMedium, "Dabboussi");
widgetView.SetTextViewText(Resource.Id.widgetSmall, oo);
}
private static string AnnouncementClick = "AnnouncementClickTag";
因此,该值显示Toast在后台的消息中,但没有显示在小部件中textview。为什么会这样,我该如何解决?
慕婉清6462132
相关分类