我正在构建一个Xamarin.Android应用程序。我有一个工作服务,我正在尝试从该服务获取数据到我有的活动。我知道人们对发布的代码过多感到不安,但我不确定我的问题在哪里。我要发布我认为必要的内容:
服务
[Service]
public class DataService : Service
{
public List<MyObjects> MyObjectList { get; private set; }
public IBinder Binder { get; private set; }
public override IBinder OnBind(Intent intent)
{
this.Binder = new DataServiceBinder(this);
return this.Binder;
}
public override void OnCreate()
{
base.OnCreate();
pendingIntent = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), 0);
StartForeground(NotificationID, GetNotification("Started Sync"));
//Do some work here to instantiate and populate the List
}
}
服务活页夹
public class DataServiceBinder : Binder
{
public DataServiceBinder(DataService service)
{
Service = service;
}
public DataService Service { get; private set; }
}
服务连接
public class DataServiceConnection : Object, IServiceConnection
{
public DataService Service { get; private set; }
public DataServiceBinder dataServiceBinder;
public bool IsConnected { get; private set; }
public void OnServiceConnected(ComponentName name, IBinder service)
{
dataServiceBinder = service as DataServiceBinder;
IsConnected = this.dataServiceBinder != null;
Service = dataServiceBinder.Service;
ServiceConnectionChanged?.Invoke(this, true);
}
public void OnServiceDisconnected(ComponentName name)
{
ServiceConnectionChanged?.Invoke(this, false);
Service = null;
}
public event EventHandler<bool> ServiceConnectionChanged;
}
活动
public class MyDataActivity : AppCompatActivity
{
private DataServiceConnection DataServiceConnection;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (DataServiceConnection == null)
{
this.DataServiceConnection = new DataServiceConnection();
}
Intent serviceToStart = new Intent(this, typeof(DataService));
BindService(serviceToStart, this.DataServiceConnection, Bind.AutoCreate);
SetContentView(Resource.Layout.MyDataLayout);
var adapter = new MyDataAdapter(DataServiceConnection.Service.MyObjectList);
}
protected override void OnResume()
{
var intent = new Intent(this, typeof(DataService));
BindService(intent, DataServiceConnection, Bind.AutoCreate);
base.OnResume();
}
protected override void OnPause()
{
UnbindService(DataServiceConnection);
base.OnPause();
}
在我的活动中,当我尝试从服务传递适配器列表时,&#34;服务&#34;是null,因为我得到一个空引用异常。为什么我的服务无效?我没有正确绑定我的服务吗?对于记录,它们的服务在应用程序上下文中启动。
答案 0 :(得分:1)
好的,我找到了解决方案。由于连接到服务是异步的,我不能指望适配器能够像这样同步获取数据:
var adapter = new MyDataAdapter(DataServiceConnection.Service.MyObjectList);
相反,我修改了我的活动,如下所示:
public class MyDataActivity : AppCompatActivity
{
private DataServiceConnection DataServiceConnection;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (DataServiceConnection == null)
{
this.DataServiceConnection = new DataServiceConnection();
}
Intent serviceToStart = new Intent(this, typeof(DataService));
BindService(serviceToStart, this.DataServiceConnection, Bind.AutoCreate);
DataServiceConnection.ServiceConnectionChanged += ServiceConnectionChanged;
SetContentView(Resource.Layout.MyDataLayout);
}
private void ServiceConnectionChanged(object sender, bool isConnected)
{
if(DataServiceConnection.Service == null)
{
return;
}
if(isConnected)
{
var adapter = new MyDataAdapter(DataServiceConnection.Service.MyObjectList);
}
}
protected override void OnResume()
{
var intent = new Intent(this, typeof(DataService));
BindService(intent, DataServiceConnection, Bind.AutoCreate);
base.OnResume();
}
protected override void OnPause()
{
UnbindService(DataServiceConnection);
base.OnPause();
}
我也修改了我的服务连接:
public class DataServiceConnection : Object, IServiceConnection
{
public DataService Service { get; private set; }
public MyDataActivity DataActivity;
public event EventHandler<bool> ServiceConnectionChanged;
public DataServiceConnection(MyDataActivity myDataActivity)
{
DataActivity = myDataActivity;
}
public void OnServiceConnected(ComponentName name, IBinder service)
{
dataServiceBinder = service as DataServiceBinder;
Service = dataServiceBinder.Service;
ServiceConnectionChanged?.Invoke(this, true);
}
public void OnServiceDisconnected(ComponentName name)
{
ServiceConnectionChanged?.Invoke(this, false);
Service = null;
}
}
现在我的解决方案只在连接服务时尝试将数据发送到适配器。这个解决方案对我有用,我希望它可以帮助其他任何人。