问题描述
我已经使用Twilio api调用了应用程序,并且在服务建立后我试图更改状态TextView文本,我搜索了很多内容,但是找不到任何有用的解决方案,我想从服务中更改文本或广播接收者 .我的服务代码如下:
I have calling app using Twilio api, and i'm trying to change status TextView text after the service established, i searched to much but i didn't find any helpful solution, i want change the text from the service or broadcast receiver .My service code below :
[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文本
The service working well and the phone connect is established well, just need to know how could i change the textView text
注意:textView在片段内部
Attention : the textView is inside fragment
推荐答案
首先,您需要在Receiver中实现此功能,而不是在Service中实现.
First of all, you need to implement this feature in Receiver, not in Service.
在您的服务中,您应该能够发送这样的文本:
In your service, you should be able to send text for example like this:
[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);
}
}
并创建您的 Receiver
和 MainActivity
,例如:
公共类MainActivity:活动{私人MyReceiver接收器;
public class MainActivity : Activity{ private MyReceiver receiver;
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
的布局中显示文本,如下所示:
I placed a Button
to start service and a TextView
to show text in the layout of Fragment
and code like this:
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;
}
}
这是结果:
这篇关于Xamarin Android:从Service或Receiver更改UI TextView文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!