gpt4 book ai didi

android - Xamarin 安卓 : Change UI TextView text from Service or Receiver

转载 作者:行者123 更新时间:2023-11-29 19:08:10 26 4
gpt4 key购买 nike

我已经使用 Twilio api 调用应用程序,我正在尝试在服务建立后更改状态 TextView 文本,我搜索了很多但没有找到任何有用的解决方案,我想更改服务或广播中的文本接收者 。我的服务代码如下:

  [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;
}


}

服务运行良好且电话连接建立良好,只需要知道如何更改 textView 文本

注意:textView 在 fragment 中

最佳答案

The service working well and the phone connect is established well, just need to know how could i change the textView text

首先,您需要在 Receiver 中实现此功能,而不是在 Service 中。

在您的服务中,您应该能够像这样发送文本:

[Service]
public class MyIntentService : IntentService
{
public MyIntentService() : base("MyIntentService")
{
}

protected override void OnHandleIntent(Intent intent)
{
//get data when service started.
var value = intent.GetStringExtra("ServiceInfo");

//send data to activity
Intent myintent = new Intent("IntentServiceAndReceiver");
myintent.PutExtra("NewInfo", "Connected...!");
SendBroadcast(myintent);
}
}

然后创建您的 ReceiverMainActivity,例如:

公共(public)类 MainActivity : Activity { 私有(private) 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);
}
}
}
}

我放置了一个 Button 来启动服务和一个 TextView 来在 Fragment 的布局中显示文本,代码如下:

public class MyFragment : Fragment
{
private TextView tv;

public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
var view = inflater.Inflate(Resource.Layout.FLayout, container, false);
tv = view.FindViewById<TextView>(Resource.Id.tv);
var btn = view.FindViewById<Button>(Resource.Id.startS);
btn.Click += (sender, e) =>
{
// This code might be called from within an Activity, for example in an event
// handler for a button click.
Intent myintent = new Intent(this.Context, typeof(MyIntentService));

// This is just one example of passing some values to an IntentService via the Intent:
myintent.PutExtra("ServiceInfo", "This is the information!");

this.Context.StartService(myintent);
};
return view;
}

public void UpdateText(string text)
{
tv.Text = text;
}
}

结果如下:

enter image description here

关于android - Xamarin 安卓 : Change UI TextView text from Service or Receiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com