[英]In Oreo (8.0.0+) (API 26+), How to get a location services update when the app is in the background or kill
在Android O(8.0.0+)(API 26+)中,如何在应用程序处于后台或终止时获取位置服务更新。 在“Strava”Android应用程序(在Play商店中可用),当应用程序在后台或终止时,位置服务可以正常运行。 我需要在我的应用程序中构建相同的功能。 每当我开始使用服务
startService(new Intent(this, GpsServices.class));
然后当应用程序处于空闲模式(应用程序在后台或终止)时,10秒后调用onDestroy方法。 以下是我的代码。
public class GpsServices extends Service implements LocationListener, Listener {
@Override
public void onCreate() {
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mLocationManager != null) {
mLocationManager.addGpsStatusListener(this);
}
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
500,
0,
this);
}
}
public void onLocationChanged(Location location) {
System.out.println("location = [" + location + "]");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
/* Remove the locationlistener updates when Services is stopped */
@Override
public void onDestroy() {
try {
mLocationManager.removeUpdates(this);
mLocationManager.removeGpsStatusListener(this);
stopForeground(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
任何帮助,将不胜感激。
您可以尝试以下两个选项之一或两者的组合,这些选项在我遇到问题时解决了我的问题。
要使位置更新继续在后台运行,必须使用带有FusedLocationProviderClient
LocationServices
API , FusedLocationProviderClient
处和此处所述 ,或者在此处使用CODEPATH。
如果您已经在这里的某个地方正确阅读了Android Oreo 8.0文档,那么您将获得此解决方案。
步骤1:确保您将服务作为前台服务启动,如下面的代码所示
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
mainActivity.startService(new Intent(getContext(), GpsServices.class));
mainActivity.startService(new Intent(getContext(), BluetoothService.class));
mainActivity.startService(new Intent(getContext(), BackgroundApiService.class));
}
else {
mainActivity.startForegroundService(new Intent(getContext(), GpsServices.class));
mainActivity.startForegroundService(new Intent(getContext(), BluetoothService.class));
mainActivity.startForegroundService(new Intent(getContext(), BackgroundApiService.class));
}
第2步:使用通知显示您的服务正在运行。 在
onCreate
服务方法中添加以下代码行。
@Override
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, notification);
}
...
}
步骤3:在服务停止或销毁时删除
notification
。
@Override
public void onDestroy() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); //true will remove notification
}
...
}
选项2的一个问题是它将继续显示notification
直到您的GpsService
在Android Oreo 8.0上运行的所有设备上运行。
我确信即使应用程序处于后台或处于kill状态,这两个选项也能正常工作。
我希望这个解决方案可以解决您的问题。
解决此问题的关键似乎是通知生成器。 以下链接描述了一个比我总结更好的工作解决方案:
https://hackernoon.com/android-location-tracking-with-a-service-80940218f561
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.