mardi 22 septembre 2015

Android location tracker crashes after a long time

I have created a location tracking app, which writes to a local SQLite database every time the location is changed. The app unfortunately crashes after about 7-8 hours while tracking, unfortunately this does not happen when I have connected the device to a debugger, so there is no log which I can attach. Some more maybe useful information:

  • The app crashes before it is waken up from the background(can see that clearly in the tracked data), so I can exclude this bug from other apps
  • Tried to write into textfiles instead of the database without any success(Just ran about 3 hours before crashing)
  • Changing tracking interval(5s normal 1s fastest interval): Same result app crashes also after 7-8 hours

Here are some code snippets:

Location change event

@Override
public void onLocationChanged(Location location) {
    if(location == null){
        location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(location == null) {
            return;
        }
    }
    Log.d(TAG, location.toString());
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    ActivitylogRepo activityRepo = new ActivitylogRepo(this);
    Activitylog activity = new Activitylog();
    activity.activity = "Position";
    activity.timestamp = getDateTime();
    activity.value2 = String.valueOf(currentLatitude);
    activity.value3 = String.valueOf(currentLongitude);
    activityRepo.insert(activity);
}

Database insert command

public int insert(Activitylog activitylog) {

    //Open connection to write data
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Activitylog.KEY_activity, activitylog.activity);
    values.put(Activitylog.KEY_timestamp, activitylog.timestamp);
    values.put(Activitylog.KEY_value1, activitylog.value1);
    values.put(Activitylog.KEY_value2, activitylog.value2);
    values.put(Activitylog.KEY_value3, activitylog.value3);
    values.put(Activitylog.KEY_value4, activitylog.value4);


    // Inserting Row
    long activitylog_id = db.insert(Activitylog.TABLE, null, values);
    db.close(); // Closing database connection
    return (int) activitylog_id;
}

Initializing service

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

    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(20 * 1000)        // 20s in ms
            .setFastestInterval(5 * 1000); // 5s in ms

Aucun commentaire:

Enregistrer un commentaire