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

标签 java android background location geo

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

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

我想问一下有什么方法可以让我在空闲模式下或当应用程序处于后台时(android OS <6.0)获取位置更新。

public class MainActivity extends AppCompatActivity {
private LocationManager locationManager;
private LocationListener locationListener;
DatabaseReference databaseValues;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    databaseValues = FirebaseDatabase.getInstance().getReference("values");
    WebView webb = (WebView) findViewById(R.id.web01);
    webb.setWebViewClient(new WebViewClient());
    webb.getSettings().setJavaScriptEnabled(true);
    webb.setWebViewClient(new WebViewClient());
    webb.setClickable(true);
    webb.setWebChromeClient(new WebChromeClient());
    webb.loadUrl("https://www.foo.com/");

    Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    startActivity(intent);

    String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if (locationProviders == null || locationProviders.equals("")) {
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    }

    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {

            String GPSLocation = location.toString();

            String Lat = GPSLocation.substring(13,21).replace(',','.');
            String Lon = GPSLocation.substring(23,31).replace(',','.');

            addData(Lat,Lon);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    if (Build.VERSION.SDK_INT < 23){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
    }else {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
        }else{
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
    }
}

即使在后台/空闲状态下,上面的代码也只能在前 30-40 分钟内运行良好。是否可以继续获取地理位置?

最佳答案

由于 location Listeners 在主线程上, Activity 可能会由于多种原因被销毁。我更喜欢在粘性服务中有位置监听器,每当我得到更新时都会将它传递给 Ui 。您可以使用监听器来完成,但我更喜欢使用实时数据从服务到 Ui 进行通信以更新最新位置并相应地更改 ui

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);
    }
}

关于java - Android:位置更新在后台一段时间后(20-30 分钟)停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464018/

相关文章:

java - Hibernate Criteria API - 创建自动连接

Java 对象转换行为异常

java - 为什么要设置字符串 DRIVER?

java - SWT 应用程序从 Eclipse IDE 运行,当我通过 Maven 创建 jar 并运行时不运行。给出 UnsatisfiedlinkException

android - 媒体编解码器 : how to measure performance?

android - 报告 Android 应用程序崩溃

android - 我们如何才能自动只在较新的 Android 版本上运行测试?

CSS 背景图片未出现在 Safari 中

css - 背景图像/颜色在 IE 中不可见

android - 获取后台服务实例