简体   繁体   English

在后台Android Oreo中的位置

[英]Location while in background Android Oreo

I want to build an app like Run Keeper for tracking while out on an actitivty. 我想构建一个类似Run Keeper的应用程序,以便在进行活动时进行跟踪。 What is the best way to track the users location while the phone is locked for example. 例如,在手机锁定时跟踪用户位置的最佳方法是什么。 The user will explicitly start the activity and will perhaps lock and unlock there phone to see there progress along the way. 用户将明确启动该活动,并可能会锁定和解锁手机,以查看进展情况。 This was relatively straight forward before oreo but now my original code will no longer function and the app stops recording location after a few minutes. 这在oreo之前是相对简单的,但是现在我的原始代码将不再起作用,并且该应用在几分钟后会停止记录位置。

At the moment I am registering a service. 目前,我正在注册服务。

startService(Intent(this, EWLocationService::class.java))

I am calling foreground in the Service with a notification: 我正在通知服务中的前台:

startForeground(1, notification)

I am sending back locations to the main app through a broadcast where the app can both add the location to the map and save it to Room database. 我正在通过广播将位置发送回主应用程序,该应用程序既可以将位置添加到地图,又可以将其保存到Room数据库。

This worked fine before, but it does not work now. 以前该方法运行良好,但现在不起作用。 What should I do to achieve the desired functionality? 我应该怎么做才能获得所需的功能?

You should read oreo update related files. 您应该阅读oreo更新相关文件。

By default, these changes only affect apps that target Android 8.0 (API level 26) or higher. 默认情况下,这些更改仅影响定位到Android 8.0(API级别26)或更高版本的应用。 However, users can enable these restrictions for any app from the Settings screen, even if the app targets an API level lower than 26. You may need to update your app to comply with the new limitations. 但是,即使该应用的目标API级别低于26,用户也可以从“设置”屏幕为任何应用启用这些限制。您可能需要更新您的应用以符合新的限制。

Check to see how your app uses services. 查看您的应用如何使用服务。 If your app relies on services that run in the background while your app is idle, you will need to replace them. 如果您的应用程序在应用程序空闲时依赖于在后台运行的服务,则需要替换它们。 Possible solutions include: 可能的解决方案包括:

If your app needs to create a foreground service while the app is in the background, use the startForegroundService() method instead of startService(). 如果您的应用程序需要在后台运行时创建前台服务,请使用startForegroundService()方法代替startService()。

If you want to know more about https://developer.android.com/about/versions/oreo/background 如果您想进一步了解https://developer.android.com/about/versions/oreo/background

For Oreo and above you must call ContextCompat.startForegroundService(this, serviceIntent); 对于Oreo及更高版本,您必须调用ContextCompat.startForegroundService(this,serviceIntent);。 while launching the service instead of startService(serviceIntent) 在启动服务而不是startService(serviceIntent)时

source code inside ContextCompat looks like this /** * startForegroundService() was introduced in O, just call startService * for before O. * * @param context Context to start Service from. * @param intent The description of the Service to start. * * @see Context#startForegroundService(Intent) * @see Context#startService(Intent) */ public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) { if (Build.VERSION.SDK_INT >= 26) { context.startForegroundService(intent); } else { // Pre-O behavior. context.startService(intent); } } ContextCompat内部的源代码如下所示: /** * startForegroundService() was introduced in O, just call startService * for before O. * * @param context Context to start Service from. * @param intent The description of the Service to start. * * @see Context#startForegroundService(Intent) * @see Context#startService(Intent) */ public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) { if (Build.VERSION.SDK_INT >= 26) { context.startForegroundService(intent); } else { // Pre-O behavior. context.startService(intent); } } /** * startForegroundService() was introduced in O, just call startService * for before O. * * @param context Context to start Service from. * @param intent The description of the Service to start. * * @see Context#startForegroundService(Intent) * @see Context#startService(Intent) */ public static void startForegroundService(@NonNull Context context, @NonNull Intent intent) { if (Build.VERSION.SDK_INT >= 26) { context.startForegroundService(intent); } else { // Pre-O behavior. context.startService(intent); } }

And inside the service you must call startForeground(int id, Notification notification) as soon as service starts 并且在服务内部,您必须在服务启动后立即调用startForeground(int id,Notification notification)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM