我正在寻找一些应用程序设计建议。
我有一个应用程序连接到Google定位服务并跟踪它收到的位置坐标。 这些在UI上显示为路径。
当屏幕超时并变空白时,该Activity将按照正常的活动生命周期关闭。
但是 - 我仍然希望记录位置服务中每个onLocationChanged事件中返回的坐标,但是,当然, Activity已暂停,因此无法执行此操作。
我并不是特别想要阻止在Manifest中消隐屏幕(因此Activity永远不会暂停)。 虽然我认为,如果收到电话等,它仍会暂停
我的解决方案是在一个Activity暂停事件( onPause , onStop或onSaveInstanceState )中启动IntentService以接收位置更新,然后当Activity重新启动时,从服务中收集数据并关闭服务。
这是否会成为实现这一目标的有效且正确的方式,还是存在一些我不知道的Android黑色艺术? 如果是这样, IntentService是否正确的方式去做(或者我应该使用Service )?
I am looking for a bit of app design advice.
I have an app that connects to the Google Location Services and tracks location coordinates it receives. These are displayed as a path on the UI.
When the screen times out and goes blank then this Activity will, of course, shut down as per the normal Activity life cycle.
However - I atill want to record the co-ordinates coming back in each onLocationChanged event from Location Services, but, of course, the Activity has paused so it cannot do that.
I don't particularly want to prevent the screen from blanking in the Manifest (and thus the Activity would never pause). Though I believe it would still pause if, say, a phone call is received etc.
My solution would be to start an IntentService in one of the Activity pausing events (either onPause, onStop or onSaveInstanceState) to receive Location updates, and then when the Activity restarts, collect the data from the Service and close the Service down.
Would this be an efficient and correct way of achieving this, or is there some Android black art that I don't know about? If so, is IntentService the correct way to go about it (or should I use Service)?
最满意答案
Proberly是一个由警报管理员重新启动的正常服务,这是一个更好的主意。
In order to tie up loose ends, I'll add my own answer to this now I have implemented something.
My solution in the end was not an IntentService. This was because I thought that the IntentService would consider its work to be the actual setting up of the LocationService and once that work had been completed then it would shut itself down rather than hang about waiting for LocationService 'pings'.
Therefore, I implemented a normal Service, but I bound it to the calling Activity. I also set up a callback to the Activity. This way when a 'ping' was received I could process it and pass back the data directly to the Activity. Also, the Service would remain alive as long as the binding was in place. I clear the binder when the Activity is destroyed.
This worked perfectly....HOWEVER
I then discovered that the reason that the LocationService 'pings' were not being handled in the Activity was bacause I was disconnecting the GoogleAPIClient when the Activity onStop was called. Having removed this, the Activity processed the 'pings'even in its stopped state, so no Service was required anyway.
Therefore the correct answer to this question is... Activity should continue processing in the background (unless it's destroyed for memory management purposes), so check you're not stopping stuff in your 'shutdown' handlers onPause onStop etc. Then you won't waste time writing services like I did!
更多推荐
发布评论