我要做的是在按下按钮的同时进行后台处理时运行活动。 (因为我真正的问题是当我有一个很长的过程时,我的前端会停止一段时间)
我尝试做的是制作一个服务并绑定它(我不知道如果我做对了,因为我认为它会独立工作)。但是,当我尝试从服务中访问该方法时,我的前景仍然停止了一段时间。
如何在等待背景中的WEB服务响应的同时实现我的前行仍在运行 (我已经做了进步条,让用户知道它仍在加载)
这是我的服务,serviceConnection和binder代码://它仍然没用,我只是想测试一下
[Service]
public class MyService : Service
{
static readonly string TAG = "X:" + typeof(MyService).Name;
int i;
#region Override OnCreate()
public override void OnCreate()
{
base.OnCreate();
Toast.MakeText(this, "MyService started", ToastLength.Short).Show();
}
#endregion
#region Override OnDestroy
public override void OnDestroy()
{
base.OnDestroy();
StopSelf();
Log.Debug(TAG, "MyService destroyed at {0}.", DateTime.Now);
}
#endregion
#region Override OnBind
public override IBinder OnBind(Intent intent)
{
return new MyBinder(this);
}
#endregion
public string GetTimestamp(string value)
{
// this is just a testing
if (value == string.Empty)
{
for (i = 0; i < 9999999; i++)
{
if (i == 100000)
Log.Debug(TAG, "100000 zxc");
if (i == 500000)
Log.Debug(TAG, "500000 zxc");
}
return "empty";
}
else
return i.ToString() + " from service";
}
}
public class MyBinder : Binder
{
MyService service;
public MyBinder(MyService service)
{
Toast.MakeText(service.ApplicationContext, "my binder", ToastLength.Short).Show();
this.service = service;
}
public string GetFormattedTimestamp(string value)
{
return service?.GetTimestamp(value);
}
}
public class MyServiceConnection : Java.Lang.Object, IServiceConnection
{
static readonly string TAG = typeof(MyServiceConnection).FullName;
public bool IsConnected { get; private set; }
public MyBinder Binder { get; private set; }
public MyServiceConnection()
{
IsConnected = false;
Binder = null;
}
public void OnServiceConnected(ComponentName name, IBinder binder)
{
Binder = binder as MyBinder;
IsConnected = Binder != null;
Log.Debug(TAG, "OnServiceConnected zxc");
}
public void OnServiceDisconnected(ComponentName name)
{
Log.Debug(TAG, "OnServiceDisconnected zxc");
IsConnected = false;
Binder = null;
}
}