为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
呼啦一阵风 2021-05-20 09:15:12
我正在构建一个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引用异常。为什么我的服务为空?我是否无法正确绑定我的服务?为了记录,它们服务在“应用程序”上下文中启动。
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

好的,我找到了解决方案。由于连接到服务是异步的,所以我不能期望适配器能够像这样同步获取数据:


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;

        }

    }

现在,我的解决方案仅在连接服务时尝试将数据发送到适配器。该解决方案对我有用,希望对其他任何人有帮助。


查看完整回答
反对 回复 2021-05-23
  • 1 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信