无法访问活动中的服务中的数据

我正在构建一个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;

    }



在我的活动中,当我尝试通过服务传递适配器列表时,“服务”为null,因此我得到了null引用异常。为什么我的服务为空?我是否无法正确绑定我的服务?为了记录,它们服务在“应用程序”上下文中启动。


呼啦一阵风
浏览 126回答 1
1回答

白衣非少年

好的,我找到了解决方案。由于连接到服务是异步的,所以我不能期望适配器能够像这样同步获取数据:var adapter = new MyDataAdapter(DataServiceConnection.Service.MyObjectList);相反,我将自己的活动修改为如下所示:public class MyDataActivity : AppCompatActivity&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private DataServiceConnection DataServiceConnection;&nbsp; &nbsp; &nbsp; &nbsp; protected override void OnCreate(Bundle savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (DataServiceConnection == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.DataServiceConnection = new DataServiceConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent serviceToStart = new Intent(this, typeof(DataService));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BindService(serviceToStart, this.DataServiceConnection, Bind.AutoCreate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataServiceConnection.ServiceConnectionChanged += ServiceConnectionChanged;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetContentView(Resource.Layout.MyDataLayout);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void ServiceConnectionChanged(object sender, bool isConnected)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(DataServiceConnection.Service == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isConnected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var adapter = new MyDataAdapter(DataServiceConnection.Service.MyObjectList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected override void OnResume()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var intent = new Intent(this, typeof(DataService));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BindService(intent, DataServiceConnection, Bind.AutoCreate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnResume();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; protected override void OnPause()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UnbindService(DataServiceConnection);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnPause();&nbsp; &nbsp; &nbsp; &nbsp; }我还对服务连接进行了如下修改:public class DataServiceConnection : Object, IServiceConnection&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DataService Service { get; private set; }&nbsp; &nbsp; &nbsp; &nbsp; public MyDataActivity DataActivity;&nbsp; &nbsp; &nbsp; &nbsp; public event EventHandler<bool> ServiceConnectionChanged;&nbsp; &nbsp; &nbsp; &nbsp; public DataServiceConnection(MyDataActivity myDataActivity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataActivity = myDataActivity;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void OnServiceConnected(ComponentName name, IBinder service)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataServiceBinder = service as DataServiceBinder;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Service = dataServiceBinder.Service;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServiceConnectionChanged?.Invoke(this, true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void OnServiceDisconnected(ComponentName name)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServiceConnectionChanged?.Invoke(this, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Service = null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }现在,我的解决方案仅在连接服务时尝试将数据发送到适配器。该解决方案对我有用,希望对其他任何人有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP