0

I have a service that when it creates and starts, initiates prerequisites of using FusedLocationAPI . buildGoogleApiClient() and createLocationRequest() with parameters that initiated by my getConfig() method from database.

This solution works great and accurate with parameters like:

UPDATE_INTERVAL= 10000 (10 sec) , FASTEST_INTERVAL= 5000 (5 sec) , DISPLACEMENT= 10 ( m )

But I need to set interval up to 5 minutes. So when i set prameters like :

UPDATE_INTERVAL= 300000 (5 min) , FASTEST_INTERVAL= 180000 (3 min) , DISPLACEMENT= 10 ( m )

GPS behaves differently and after 30 seconds stops searching GPS and location notification that blinks, disappears.

And starts searching again after device locked and unlocked or this Service stops and starts again.

Here is my service. Please tell me why searching GPS stops right after 30 seconds.

Thanks in advanece.

public class MainService extends Service implements ConnectionCallbacks,
    OnConnectionFailedListener, LocationListener {

private final static String TAG = MainActivity.class.getSimpleName();

private static int UPDATE_INTERVAL;
private static int FASTEST_INTERVAL;
private static int DISPLACEMENT;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

private Location mLastLocation;

public String deviceIMEI;
public boolean isGpsEnabled = false;

DatabaseHelper mDBHelper;

@Override
public IBinder onBind(Intent intent) {

    return null;
}

@Override
public void onCreate() {

    super.onCreate();

    getDeviceIMEI();
    getConfig();
    startForeGroundService();

    if (checkPlayServices() && checkLocationServices()) {

        buildGoogleApiClient();
        createLocationRequest();
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();

    else {

        stopForeground(true);
        stopSelf();
    }

    return (START_NOT_STICKY);
}

@Override
public void onDestroy() {

    stopForeground(true);

    if (mGoogleApiClient != null)
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient, this);

    mGoogleApiClient.disconnect();
}

@SuppressWarnings("deprecation")
public void startForeGroundService() {

    Notification note = new Notification(
            R.drawable.track_notification_alert,
            "GPS tracking started ...", System.currentTimeMillis());

    Intent i = new Intent();

    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

    note.setLatestEventInfo(this, "GPS is active.",
            "Now this tablet is under tracking. ", pi);

    note.flags |= Notification.FLAG_NO_CLEAR;

    startForeground(1337, note);
}

protected synchronized void buildGoogleApiClient() {

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();
}

protected void createLocationRequest() {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

private boolean checkLocationServices() {

    LocationManager lm = (LocationManager) getApplicationContext()
            .getSystemService(LOCATION_SERVICE);

    isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

    return isGpsEnabled;
}

private boolean checkPlayServices() {

    int resultCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(this);

    if (resultCode != ConnectionResult.SUCCESS) {

        return false;
    }

    return true;
}

@Override
public void onConnected(Bundle arg0) {

    LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

    if (Globals.isSatelliteFix && Globals.isGPSEnable)
        setPoint();
}

@Override
public void onConnectionSuspended(int arg0) {

    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult result) {

    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());
}

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;

    if (Globals.isSatelliteFix && Globals.isGPSEnable)
        setPoint();
}

private void setPoint() {

    mDBHelper = new DatabaseHelper(getApplicationContext());

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {

        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        try {

            Point p = new Point();

            p.setId(UUID.randomUUID().toString());
            p.setIMEI(deviceIMEI);
            p.setLatitude(String.valueOf(latitude));
            p.setLongitude(String.valueOf(longitude));
            p.setTimeStamp(createTimeStamp());

            long result = mDBHelper.addPoint(p);

            if (result != -1) {

                Log.i(TAG, "Insertion Successfull With RowId: " + result);

            } else
                Log.e(TAG, "Insertion Error");

        } catch (Exception e) {

            e.printStackTrace();
            Log.e(TAG, "Insertion Error: " + e.getMessage());
        }
    }
}

public String createTimeStamp() {

    String strTimeStamp = new SimpleDateFormat("yyyy-MM-dd_kk:mm:ss")
            .format(new Date());

    return strTimeStamp;
}

public void getDeviceIMEI() {

    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    deviceIMEI = telephonyManager.getDeviceId();
}

public long getTimeInMiliSeconds() {

    Calendar calendar = Calendar.getInstance();
    Date date = calendar.getTime();
    return date.getTime();
}

public void getConfig() {

    Config config = new Config();
    mDBHelper = new DatabaseHelper(getApplicationContext());

    config = mDBHelper.getConfig();

    UPDATE_INTERVAL = config.getUpdateInterval();
    FASTEST_INTERVAL = config.getFastestInterval();
    DISPLACEMENT = config.getSmallestDisplacement();

}

}

4

0 回答 0