Java Android:位置更新在后台经过一段时间(20-30分钟)后停止

Java Android:位置更新在后台经过一段时间(20-30分钟)后停止,java,android,background,location,geo,Java,Android,Background,Location,Geo,我正在构建一个web应用程序来获取位置更新。我知道在最新的Android操作系统(oreo)中,该服务需要在前台才能获得位置更新。但目前的问题是关于操作系统的早期版本(

我正在构建一个web应用程序来获取位置更新。我知道在最新的Android操作系统(oreo)中,该服务需要在前台才能获得位置更新。但目前的问题是关于操作系统的早期版本(<6.0)。 即使应用程序在后台运行,我也会在20-30分钟内每秒更新一次位置。但是这些更新突然停止了

我还尝试将该应用程序作为后台服务来收集更新,但未能获得用户对后台服务的同意


我想问一下,是否有一种方法可以在空闲模式下或应用程序处于后台时获取位置更新(android OS由于位置监听器位于主线程上,活动可能会由于多种原因而被破坏。我更喜欢在sticky服务中使用位置监听器,当我得到更新时,会将其传递到Ui。你可以使用监听器来完成,但我更喜欢使用实时数据从服务到Ui进行通信,以更新最新的locat并相应地更改用户界面

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service
{
private static final String TAG = "LocationService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 10000;
private static final float LOCATION_DISTANCE = 50f;

private class LocationListener implements android.location.LocationListener
{
    Location mLastLocation;

    public LocationListener(String provider)
    {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location)
    {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}

LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER)
};

@Override
public IBinder onBind(Intent arg0)
{
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.e(TAG, "onStartCommand");
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onCreate()
{
    Log.e(TAG, "onCreate");
    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[1]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "gps provider does not exist " + ex.getMessage());
    }
}

@Override
public void onDestroy()
{
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                mLocationManager.removeUpdates(mLocationListeners[i]);
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    }
}
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.util.Log;
公共类MyService扩展服务
{
私有静态最终字符串TAG=“LocationService”;
私有位置管理器mLocationManager=null;
专用静态最终整数位置\u间隔=10000;
专用静态最终浮动位置_距离=50f;
私有类LocationListener实现android.location.LocationListener
{
位置mLastLocation;
公共位置侦听器(字符串提供程序)
{
Log.e(标记“LocationListener”+提供者);
mLastLocation=新位置(提供商);
}
@凌驾
已更改位置上的公共无效(位置)
{
Log.e(标签“onLocationChanged:+位置);
mLastLocation.set(位置);
}
@凌驾
公共无效onProviderDisabled(字符串提供程序)
{
Log.e(标记“onProviderDisabled:+提供程序”);
}
@凌驾
公共无效onProviderEnabled(字符串提供程序)
{
Log.e(标记“onProviderEnabled:+提供程序”);
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra)
{
Log.e(标记“onStatusChanged:+提供者”);
}
}
LocationListener[]mlLocationListeners=新LocationListener[]{
新LocationListener(LocationManager.GPS_提供程序),
新LocationListener(LocationManager.NETWORK\u提供程序)
};
@凌驾
公共IBinder onBind(意图arg0)
{
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId)
{
Log.e(标记“onStartCommand”);
super.onStartCommand(intent、flags、startId);
返回开始时间;
}
@凌驾
public void onCreate()
{
Log.e(标记为“onCreate”);
初始化ElocationManager();
试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.NETWORK_提供程序、位置间隔、位置距离、,
mLocationListeners[1]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“网络提供商不存在”+ex.getMessage());
}
试一试{
mLocationManager.RequestLocationUpdate(
LocationManager.GPS\u提供程序、位置\u间隔、位置\u距离、,
mLocationListeners[0]);
}catch(java.lang.SecurityException ex){
Log.i(标记“无法请求位置更新,忽略”,例如);
}捕获(IllegalArgumentException ex){
Log.d(标记“gps提供程序不存在”+ex.getMessage());
}
}
@凌驾
公共空间
{
Log.e(标签“onDestroy”);
super.ondestory();
if(mLocationManager!=null){
for(int i=0;i

}

我尝试将其作为一项服务,并在主要活动中通过广播接收器接收。但以某种方式作为服务运行并没有激活我的GPS。我甚至没有在通知中看到GPS标志。作为服务运行时,同意存在一些问题。