Xamarin Android : Change UI TextView text from Service or Receiver


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

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

Attention : the textView is inside fragment



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


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:

<pre class="lang-cs prettyprint-override">[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); } }

And create your Receiver and MainActivity for example like this:

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

I placed a Button to start service and a TextView to show text in the layout of Fragment and code like this:

<pre class="lang-cs prettyprint-override">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; } }

Here is the result:

<a href="https://i.stack.imgur.com/5D9gz.gif" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/5D9gz.gif" data-original="https://i.stack.imgur.com/5D9gz.gif" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>


  • Android: How do you create an EditText field in java class
  • Get the last date of each month in a list of dates in Python
  • CloseOptionsMenu doesn't work?
  • Unable to resolve Static method?
  • Receiver has no segue with identifier“***”
  • Main Thread Checker: UI API called on a background thread iOS 11 Xcode 9 GM Seed
  • Activation Function choice for Neural network
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • WPF version of .ScaleControl?
  • How can I replace the server in Web Component Tester
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • custom UITableViewCell with image for highlighting
  • Avoid links criss cross / overlap in d3.js using force layout
  • Recording logins for password protected directories
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • jquery mobile loadPage not working
  • Web-crawler for facebook in python
  • align graphs with different xlab
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • unknown Exception android
  • Checking variable from a different class in C#
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize