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

时间:2020-08-31 06:54:33

标签: android service location android-10.0

我遇到了Android 10中的问题,即当后台应用或应用被杀死时,每10分钟要获取一次位置。直到android 9 pie一切都工作正常,但是在android 10中,当应用在几秒钟内进入后台时,在服务中调用了onDestory()方法,并且服务正在销毁。我该如何解决这个问题并在后台启动服务。这是我的服务班级:

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

}

1 个答案:

答案 0 :(得分:1)

由于android在post-O设备中执行后台任务的局限性,如果应用在后台运行一段时间,则android系统会破坏后台服务。
您应在{中使用startForeground() {1}}将该服务作为前台服务启动,并显示有关该服务的通知。

相关问题