如何为后台工作创建绑定服务(Xamarin)

时间:2015-08-10 13:49:57

标签: android .net xamarin

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

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

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

public override IBinder OnBind (Intent intent)
        {

            return null;
        }

以下是完整代码:

[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 个答案:

答案 0 :(得分:4)

// standard algorithms QFileInfoList fileInfoListFromPaths(const QStringList & list) { QFileInfoList result; result.reserve(list.size()); std::copy(list.begin(), list.end(), std::back_inserter(result)); return result; } // explicit iteration QFileInfoList fileInfoListFromPaths(const QStringList & list) { QFileInfoList result; result.reserve(list.size()); #if 1 // C++11 for (path : list) result << QFileInfo(path); #else // or C++98 foreach (path, list) result << QFileInfo(path); #endif return result; } 应该返回绑定的实例,或者在您的情况下返回OnBind。当您的活页夹应该从MyBinder继承时,它正在实施ServiceConnectionBinder是另一个难题。你的活页夹应该是这样的:

ServiceConnection

我建议您在Xamarin阅读此3 part tutorial,其中涵盖了Android绑定服务主题。