Xamarin Android:更改UI文本查看来自服务或接收器的文本
我使用Twilio api调用应用程序,我试图在服务建立后更改状态文本视图文本,我搜索了很多,但没有找到任何有用的解决方案,我想更改服务或广播接收器的文本。 我的服务代码如下:Xamarin Android:更改UI文本查看来自服务或接收器的文本,android,xamarin,xamarin.android,Android,Xamarin,Xamarin.android,我使用Twilio api调用应用程序,我试图在服务建立后更改状态文本视图文本,我搜索了很多,但没有找到任何有用的解决方案,我想更改服务或广播接收器的文本。 我的服务代码如下: [Service] class CallService : IntentService { public static MonkeyPhone phone ; protected override void OnHandleIntent(Intent inten
[Service]
class CallService : IntentService
{
public static MonkeyPhone phone ;
protected override void OnHandleIntent(Intent intent)
{
throw new NotImplementedException();
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
// countine
new Task(() =>
{
phone = new MonkeyPhone(ApplicationContext);
View view = View.Inflate(ApplicationContext, Resource.Layout.Main, null);
TextView connectionStatus = view.FindViewById<TextView>(Resource.Id.connectionStatus);
connectionStatus.Text = "Connected ..";
}).Start();
return StartCommandResult.Sticky;
}
}
[服务]
类CallService:IntentService
{
公共静态手机;
受保护的覆盖无效OnHandleIntent(意图)
{
抛出新的NotImplementedException();
}
公共覆盖StartCommandResult OnStartCommand(意图、StartCommandFlags标志、int-startId)
{
//伯爵
新任务(()=>
{
phone=新的MonkeyPhone(ApplicationContext);
View=View.Inflate(ApplicationContext,Resource.Layout.Main,null);
TextView connectionStatus=view.findviewbyd(Resource.Id.connectionStatus);
connectionStatus.Text=“Connected..”;
}).Start();
返回StartCommandResult.Sticky;
}
}
服务很好,电话连接也很好,只需要知道如何更改textView文本
注意:文本视图在片段内部
服务很好,电话连接也很好,只需要知道如何更改textView文本
首先,您需要在接收器中实现此功能,而不是在服务中
在您的服务中,您应该能够发送文本,例如:
[服务]
公共类MyIntentService:IntentService
{
公共MyIntentService():基(“MyIntentService”)
{
}
受保护的覆盖无效OnHandleIntent(意图)
{
//在服务启动时获取数据。
var值=intent.GetStringExtra(“ServiceInfo”);
//向活动发送数据
意向myintent=新意向(“意向服务和接收者”);
myintent.PutExtra(“NewInfo”,“Connected…!”);
发送广播(myintent);
}
}
并创建您的接收器
和main活动
,例如:
公共课活动:活动
{
私人MyReceiver
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
receiver = new MyReceiver(this);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//show fragment in framelayout container
FragmentTransaction ft = this.FragmentManager.BeginTransaction();
var myfragment = new MyFragment();
ft.Add(Resource.Id.container, myfragment).AddToBackStack(null).Commit();
}
protected override void OnResume()
{
base.OnResume();
RegisterReceiver(receiver, new IntentFilter("IntentServiceAndReceiver"));
}
protected override void OnPause()
{
UnregisterReceiver(receiver);
base.OnPause();
}
[BroadcastReceiver(Enabled = true, Exported = false)]
[IntentFilter(new[] { "IntentServiceAndReceiver" })]
public class MyReceiver : BroadcastReceiver
{
private Activity mactivity;
public MyReceiver()
{
}
public MyReceiver(Activity activity)
{
mactivity = activity;
}
public override void OnReceive(Context context, Intent intent)
{
var value = intent.GetStringExtra("NewInfo");
//update textview in fragment
if (mactivity != null)
{
var myfragment = mactivity.FragmentManager.FindFragmentById<MyFragment>(Resource.Id.container);
myfragment.UpdateText(value);
}
}
}
}
结果如下: