从 Android 应用程序启动和停止 GPS(不是从单个 Activity)
我有一个应用程序,我需要一个或多个活动的当前 GPS 位置,为了减少用户的等待时间,我尝试在应用程序启动时启动 GPS LocationListener。我希望当我的应用程序从任何活动退出(转到主屏幕)时,GPS 监听器应该停止。我有多项活动,任何活动都可以使用 GPS,但由于我的应用程序流程,我们不确定用户将从哪个活动退出,所以我想维护 GPS 的启动和停止的一些全局状态。基本上将启动和停止与应用程序启动和停止绑定。
我正在寻找一些具体的解决方案,任何指示都会很棒..请指导!
I have an App where I need current GPS location by one or many Activities, In an attempt to reduce the waiting time for the user, I am trying to start the GPS LocationListener at the App launch. I want when my App exits (Go to Home screen) from any of the activity, GPS listener should be stop. I am having several activities and any activity can use GPS but due to my App flow we are not sure from which activity user would exit So I want to maintain some global state of the GPS for Start and Stop. Basically binding the Start and Stop with App Start and Stop.
I am looking for some concrete solution for this, Any pointers would be great..Please guide!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来不是一个好主意。您应该在 Activity 级别管理 GPS 侦听器 - 在
onResume
中附加它们并在onPause
中分离。我认为问题在于您有很多活动,并且您想在每个活动中做相同的事情。只需创建一个基本的抽象 Activity 类来管理 GPS 连接/断开连接并扩展该类的每个活动。That doesn't sound like a good idea. You should manage GPS listeners on Activity level - attach them in
onResume
and detach inonPause
. I believe the problem is that you have many Activities and you want to do the same stuff in every of them. Just create a base abstract Activity class to manage GPS connect/disconnect and extend every activity from this class.尝试使用 denis.solonenko 的想法来处理这个问题。如果您仍然想保持自己的方式,可以尝试使用
startActivityForResult()
函数来获取子活动返回主活动时的信息。Try to use denis.solonenko idea to handle that. If you still want to keep your own way you could try using the
startActivityForResult()
function to get information when a child activity returns to your main activity.