gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:47:45 25 4
gpt4 key购买 nike

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

   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
}

最佳答案

OnBind 应该返回 Binder 的一个实例,或者在您的情况下返回 MyBinder。当您的 Binder 应该继承自 Binder 时,它正在实现 ServiceConnectionServiceConnection 是另一 block 拼图。你的 Binder 应该看起来像这样:

public class MyServiceBinder : Binder
{
private readonly MyService _service;

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

public MyService GetMyService()
{
return _service;
}
}

我建议阅读此 3 part tutorial Xamarin 很好地涵盖了 Android 绑定(bind)服务主题。

关于android - 如何为后台工作创建绑定(bind)服务 (Xamarin),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com