android 10 中的后台服务每 10 分钟获取一次位置

Background service in android 10 to get location in every 10 minutes

我在 Android 10 中遇到问题,当应用程序在后台或应用程序被杀死时,每 10 分钟获取一次位置。直到 android 9 pie 一切正常,但在 android 10 中,当应用程序在几秒钟内进入后台时在服务中调用 onDestory() 方法并且服务正在破坏。我怎样才能解决这个问题并在后台启动服务。这是我的服务 class :

public class LocaionTrackingService extends Service {
private static final String TAG = "LocaionTrackingService";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = Common.LOCATION_TIME_INTERVAL;
private static final float LOCATION_DISTANCE = 0;
private Context mContext;
boolean checkGPS = false;
boolean checkNetwork = false;
Location loc;
public static final int notify = Common.LOCATION_TIME_INTERVAL;  //interval between two services(Here Service run every 5 Minute)
private Handler mHandler = new Handler();   //run on another Thread to avoid crash
private Timer mTimer = null;

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

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

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Common.printLog(TAG, "onLocationChanged: " + location + "\n" + "Lat:" + location.getLatitude() + "\nLang:" + location.getLongitude());
            mLastLocation.set(location);
            try {
                Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                if (addresses.size() > 0) {
                    String address = Common.getFullAddressFromGeoCoder(addresses.get(0));
                    PreferenceData.setLocationData(String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), address);
                    Common.printLog(TAG, "->address" + address);
                }
                sendLatLong(location.getLatitude() + "", location.getLongitude() + "", PreferenceData.getAddress(), Common.isGPSON(getApplicationContext()) ? "1" : "0");

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

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

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

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

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

@Override
public IBinder onBind(Intent arg0) {
    throw new UnsupportedOperationException("Not yet implemented");

}

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

@Override
public void onCreate() {
    Common.printLog(TAG, "onCreate");
    initializeLocationManager();
    setLastLocation();

    if (mTimer != null) // Cancel if already existed
        mTimer.cancel();
    else
        mTimer = new Timer();   //recreate new
    mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify);   //Schedule task

}

void getAddressfromGeocoder(double Latitude, double Longitude) {
    try {
        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
        List<Address> addresses = null;
        addresses = geocoder.getFromLocation(Latitude, Longitude, 1);

        if (addresses.size() > 0) {
            String address = Common.getFullAddressFromGeoCoder(addresses.get(0));
            PreferenceData.setLocationData(String.valueOf(Latitude), String.valueOf(Longitude), address);
            Common.printLog(TAG, "tag->getAddressfromGeocoder :" + address + "\nLatitude" + Latitude + "\nLongitude" + Longitude);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

void setLastLocation() {
    try {
        if (!checkGPS && !checkNetwork) {
            //Toast.makeText(mContext, "No Service Provider is available", Toast.LENGTH_SHORT).show();
        } else {
            if (checkGPS && !PreferenceData.getLastLAN().equals("") && !PreferenceData.getLastLAT().equals("")) {
                Common.printLog(TAG, "check For GPS");
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[0]);
                if (mLocationListeners != null) {
                    loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (loc != null) {
                        getAddressfromGeocoder(loc.getLatitude(), loc.getLongitude());
                    }
                }

            } else if (checkNetwork) {
                Common.printLog(TAG, "check For Network");
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[1]);

                if (mLocationListeners != null) {
                    loc = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (loc != null) {
                        getAddressfromGeocoder(loc.getLatitude(), loc.getLongitude());
                    }
                }
            }

        }
    } catch (java.lang.SecurityException ex) {
        Common.printLog(TAG, "fail to request location update, ignore" + ex);
    } catch (IllegalArgumentException ex) {
        Common.printLog(TAG, "gps provider does not exist " + ex.getMessage());
    }
}

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

private void initializeLocationManager() {
    Common.printLog(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        // get GPS status
        checkGPS = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // get network provider status
        checkNetwork = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }
}

void sendLatLong(final String Lat, final String Long, final String address, final String isLocationOn) {
    if (NetworkUtil.getConnectivityStatus(getApplicationContext()) != 0) {
        String url = Common.LIVE_EMP_TRACK;
        Common.printLog(TAG, "url->" + url);
        StringRequest strReq = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Common.printLog(TAG + "->response", response);
                PreferenceData.setLocationData(Lat, Long, address);
                PreferenceData.setLastUpdateLocation(System.currentTimeMillis());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //Common.printLog(TAG, String.valueOf(Common.getErrorMsg(mContext, error, null)));
                PreferenceData.setLastUpdateLocation(System.currentTimeMillis());
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Latitude", Lat);
                params.put("Longitude", Long);
                params.put("Location", address);
                params.put("isLocationOn", isLocationOn);
                params.put("EmpId", PreferenceData.getUserBy());
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("API-Key", BuildConfig.KEY);
                params.put("Clientip", PreferenceData.getIpAddress());
                params.put("Userid", PreferenceData.getUserId());
                return params;
            }
        };

        VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq);
    } else {
        Common.printLog("Service", "No network");
    }
}

//class TimeDisplay for handling task
class TimeDisplay extends TimerTask {
    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                setLastLocation();
                Common.printLog(TAG, "Location Service running-" + Calendar.getInstance().getTimeInMillis());
                //Toast.makeText(LocaionTrackingService.this, "Service is running", Toast.LENGTH_SHORT).show();
                sendLatLong(PreferenceData.getLastLAT(), PreferenceData.getLastLAN(), PreferenceData.getAddress(), Common.isGPSON(getApplicationContext()) ? "1" : "0");
            }
        });
    }
}

}

由于 android 在 post-O 设备中对后台任务的限制,如果应用程序在后台运行一段时间,后台服务将被 android 系统破坏。
您应该在 onStartCommand() 中使用 startForeground() 将服务作为前台服务启动,并显示相同的通知。