应用程序最小化时,位置更新无法正常运行

时间:2017-12-18 17:05:26

标签: android android-service locationlistener

我已经实现了一个LocationListener,以便在app最小化时接收服务中的定期更新。但是这不起作用。只有在应用程序可见时才会收到更新。屏幕锁定或应用程序最小化时,位置更新停止。

public class MapService1 extends Service {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
protected LocationManager locationManager;

Location location;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener());
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        System.out.println("----------------------NEW ON LOCATION CHANGED (" + location.getLatitude() + "," + location.getLongitude() + ")----------------------");
    }

    public void onStatusChanged(String s, int i, Bundle b) {
    }

    public void onProviderDisabled(String s) {
    }

    public void onProviderEnabled(String s) {
    }
}
}

1 个答案:

答案 0 :(得分:0)

将此权限添加到您的清单文件

    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

您的问题很可能是您的应用程序在使用该应用程序时只有位置权限。这将使您可以选择始终接受位置权限。