2017-09-25 42 views
1

我有調用應用程序使用Twilio api,並且我試圖改變狀態TextView文本服務建立後,我搜索了很多,但我沒有找到任何有用的解決方案,我想更改服務或廣播接收器的文本。 我下面的服務代碼:Xamarin Android:更改服務或接收器的UI 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的是內部片段

回答

2

服務運行良好,手機連接建立良好,只需要知道如何更改textView文本

首先,您需要在Receiver中實現此功能,而不是在服務中。

在你的服務,你應該能夠發送文字,例如像這樣:

[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例如像這樣:

公共類MainActivity:活動 { 私人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

+0

非常感謝你,它運作良好! –