I need a service which will work on background mode.Im successful create a Start service, but he dies when im close the app. Im trying to create a bound service to resolve this problem

我需要一个可以在后台模式下工作的服务。我成功创建了一个Start服务,但是当我关闭应用程序时他就死了。我试图创建一个绑定服务来解决这个问题

   var demoServiceIntent = new Intent (this, typeof(MyService));
    var demoServiceConnection = new MyBinder (this);
    ApplicationContext.BindService (demoServiceIntent, demoServiceConnection, Bind.AutoCreate)

What i need to write in this method:

我需要用这种方法写一下:

public override IBinder OnBind (Intent intent)
        {

            return null;
        }

Here is the full code:

这是完整的代码:

[Activity (Label = "really", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        public Communicator communicator;

        protected override void OnCreate (Bundle bundle)
        {   
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.BlueTooth);
            communicator = new Communicator (this);
            var messageButton = FindViewById<Button> (Resource.Id.messageButton);
            messageButton.Click += delegate(object sender, EventArgs e) {
                communicator.SendMessage ("time: " + DateTime.Now.ToString ("T"));
            };
            communicator.MessageReceived += message => RunOnUiThread (() => messageButton.Text = message);
            var dataButton = FindViewById<Button> (Resource.Id.dataButton);
            dataButton.Click += delegate {
                var dataMap = new DataMap ();
                dataMap.PutString ("time", DateTime.Now.ToString ("T"));
                communicator.SendData (dataMap);
            };
            communicator.DataReceived += dataMap => RunOnUiThread (() => dataButton.Text = dataMap.ToString ());

            var demoServiceIntent = new Intent (this, typeof(MyService));
            var demoServiceConnection = new MyBinder (this);
            ApplicationContext.BindService (demoServiceIntent, demoServiceConnection, Bind.AutoCreate);
        }

        protected override void OnResume ()
        {
            base.OnResume ();

            communicator.Resume ();
        }

        protected override void OnPause ()
        {
            communicator.Pause ();

            base.OnPause ();
        }

        protected override void OnStart ()
        {
            base.OnStart ();

        }



    }

    [Service]
    [IntentFilter (new String[]{ "com.xamarin.MyService" })]
    public class MyService : Service
    {
        Context mContext;

        public MyService ()
        {
            mContext = Android.App.Application.Context;
        }

        [Obsolete ("deprecated")]
        public override  StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
        {
            return StartCommandResult.Sticky;
        }

        #region implemented abstract members of Service

        public override IBinder OnBind (Intent intent)
        {
            return null;
        }

        #endregion

        public override void OnDestroy ()
        {
            base.OnDestroy ();
            // cleanup code
        }

    }

    public class MyBinder : Java.Lang.Object, IServiceConnection
    {
        private Context mnContext;

        public MyBinder ()
        {
            mnContext = Android.App.Application.Context;
        }

        public MyBinder (Context context)
        {
            mnContext = context;
        }

        #region IServiceConnection implementation

        public void OnServiceConnected (ComponentName name, IBinder service)
        {
        }

        public void OnServiceDisconnected (ComponentName name)
        {
        }

        #endregion
    }

1 个解决方案

#1


4

OnBind should return an instance of the binder, or MyBinder in your case. Your binder is implementing a ServiceConnection when it should be inheriting from Binder instead. The ServiceConnection is another piece of the puzzle. Your binder should look something like this:

OnBind应该返回绑定器的实例,或者在你的情况下返回MyBinder。当它应该从Binder继承时,你的绑定器正在实现一个ServiceConnection。 ServiceConnection是另一个难题。你的活页夹应该是这样的:

public class MyServiceBinder : Binder
{
    private readonly MyService _service;

    public DemoServiceBinder(MyService service)
    {
        _service = service;
    }

    public MyService GetMyService()
    {
        return _service;
    }
}

I suggest reading this 3 part tutorial at Xamarin which covers the Android Bound Services topic very well.

我建议在Xamarin上阅读这个3部分的教程,它非常清楚地介绍了Android绑定服务主题。

更多相关文章

  1. javascript(六)js事件绑定浏览器兼容解决方案 attachEvent addEve
  2. 使用SWT/JFace与WindowBuilder绑定数据的参考资料

随机推荐

  1. 5月小复盘
  2. 如何进行数据图形化?
  3. 2020年Python文章盘点,我选出了个人TOP10
  4. 如何做好描述统计分析
  5. SQL今日一题(5):一题多解
  6. 数据分析中会常犯哪些错误,如何解决?
  7. 数学之美:数学究竟是如何被运用到生活中的
  8. SQL今日一题(16):3表连接
  9. SQL今日一题(10)
  10. SQL今日一题(11):窗口函数