2017-09-25 13 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
の内側にあります

サービスがうまく機能し、電話接続が確立されています。テキストビューのテキストを変更する方法を知る必要があります。

まず、この機能をサービスではなくレシーバーに実装する必要があります。このように、たとえば

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

私はFragmentと、このようなコードのレイアウトでテキストを表示するサービスやTextViewを開始するButtonを置い:

enter image description here

+0

:ここ

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

は結果であり、そんなにうまく働いてくれてありがとう! –

関連する問題