Xamarin Android:从服务或接收器更改UI TextView文本

时间:2017-09-25 16:05:24

标签: android xamarin xamarin.android

我使用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在片段

1 个答案:

答案 0 :(得分: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:Activity {     私人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来启动服务,TextViewFragment的布局中显示文字,代码如下:

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