Search code examples
wear-oswakelockbackground-servicesensormanager

Background service stops working when disconnected from android studio debugger (Android wear)


I have a one fragment activity and one background service which implements SensorEventListener. service is working perfectly even when the screen is off and calls my fragment activity after a right argument. the only problem is when i disconnect my wearable from debugger or cellphone (can not see the logcat anymore). the service stops working! and the service wont trigger the fragment activity anymore. any help could be lifesaver. Thanx in advance

public class myService extends Service implements SensorEventListener {
  private SensorManager mSensorManager;
  private Sensor mAccelerometer;
  ...
  public int onStartCommand(Intent intent, int flags, int startId) {
    //USING SENSOR MANAGER
    mSensorManager = mSensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer, mSensorManager.SENSOR_DELAY_FASTEST);
    return START_STICKY;
  }
...
public void onSensorChanged(SensorEvent event) {
  ...
  if(event.sensor.getStringType().equals(Sensor.STRING_TYPE_ACCELEROMETER)) {
    //CONVERTING VALUES
    mXValuesAcc = (float)(((int)(event.values[0] * 100)) / 100.0);
    mYValuesAcc = (float)(((int)(event.values[1] * 100)) / 100.0);
    mZValuesAcc = (float)(((int)(event.values[2] * 100)) / 100.0);

    //ADDING THREE ACCELEROMETER DATA TOGETEHR
    sumXYZ = Math.abs(mXValuesAcc) + Math.abs(mYValuesAcc) + Math.abs(mZValuesAcc);

    //MAKING READY TO CALL THE MAIN FRAGMENT ACTIVITY 
    Intent MainIntent = new Intent(this, myActivity.class);
    MainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    //SENDING THE sumXYZ VALUE TO THE winLooseState FUNCTION
    WinLooseState winLooseState = myAlgo(sumXYZ, eventTimestamp);
    //AS A RESULT STATE OF EVENT WILL BE DEFINED

    switch (winLooseState) {
      case WIN:
        ...
        break;

      case LOOSE:
        //MAKING PowerManager READY
        PowerManager TempPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock TempWakeLock = TempPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
          PowerManager.ON_AFTER_RELEASE, "TempWakeLock");
        // LCD ON
        TempWakeLock.acquire();

        //DATA TO SEND TO myActivity
        MainIntent.putExtra(...);
        //CALLING myActivity                    
        startActivity(MainIntent);

        // LCD off  
        TempWakeLock.release();
        break;

      case DRAW:
        ...
    }
  }
}
}


Solution

  • Sorry for the late answer. so the solution is nothing more than the second wakelock() actually. as i found the solution i was beating my self for a week(inside my own head). it was a stupid mistake but so sweet when it was solved :)

    so :

    @Override
    public void onCreate() {    
        PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "systemService");
    }
    

    and under onStartCommand(..)

    just add:

    mWakelock.acquire();
    

    so at the end two wakelocks hope it helps!