【问题标题】:Android Google Play Location Services continue after leaving appAndroid Google Play 位置服务在离开应用程序后继续
【发布时间】:2013-09-10 02:16:23
【问题描述】:

我在我的应用程序中使用Google Play Services,以便我可以使用Location 功能。我已阅读文档,但不清楚当我运行位置更新并离开应用程序时会发生什么。

位置更新是否仍会在后台进行?如果没有,我该怎么做?

【问题讨论】:

    标签: java android google-play-services android-location


    【解决方案1】:

    位置更新是否仍会在后台进行?如果没有,我该怎么做?

    我为我的应用所做的就是继续使用后台服务跟踪位置。此外,您可能希望启动该服务并将其标记为前台,以便向用户显示您正在跟踪手机位置的通知。

    【讨论】:

    • 有趣。您能否详细说明如何将服务标记为前台,以便显示正在跟踪位置的通知?
    • 另外,对如何制作Service 并在后台更新它有点困惑。
    • 要标记为前景,只需在 onCreate 中使用 startForeground(R.string.app_name, aNotification)。
    • 关于服务,如果您是新手,您应该阅读 Android 的指南。 developer.android.com/guide/components/services.html
    【解决方案2】:

    请确保在您离开应用程序时删除LocationListener,通常我将其放在ActivityonPause() 方法中,否则由您自己处理是否应该这样做。下面是示例代码:

    @Override   
    protected void onPause() {      
    locationMananger.removeUpdates(this);       
    // with "this" is your Activity implementing the LocationListener       
    super.onPause();    
    }
    

    希望这会有所帮助。

    【讨论】:

    • 好吧,既然我的应用程序是在离开应用程序时继续位置更新,我就不需要做任何事情了吗?
    • 您应该考虑开发您的Service 以使用单独的进程在后台运行并更新您的位置。这里是从Service: vogella.com/articles/AndroidServices/article.html 开始的地方。希望这会有所帮助。
    猜你喜欢
    • 2017-04-17
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2019-09-15
    • 2018-05-11
    相关资源
    最近更新 更多