与 android studio 调试器断开连接时后台服务停止工作(Android 磨损)
Background service stops working when disconnected from android studio debugger (Android wear)
我有一个片段 activity 和一个实现 SensorEventListener 的后台服务。即使屏幕关闭并在正确的参数后调用我的片段 activity ,服务也能正常工作。唯一的问题是当我断开可穿戴设备与调试器或手机的连接时(再也看不到 logcat)。服务停止工作!并且该服务不会再触发片段 activity。任何帮助都可以救命。提前致谢
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:
...
}
}
}
}
抱歉回答晚了。
所以解决方案实际上只不过是第二个 wakelock() 。当我找到解决方案时,我已经击败了自己一个星期(在我自己的脑海中)。这是一个愚蠢的错误,但当它被解决时,真是太好了:)
所以 :
@Override
public void onCreate() {
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "systemService");
}
和 onStartCommand(..)
只需添加:
mWakelock.acquire();
所以最后有两个唤醒锁
希望对您有所帮助!
我有一个片段 activity 和一个实现 SensorEventListener 的后台服务。即使屏幕关闭并在正确的参数后调用我的片段 activity ,服务也能正常工作。唯一的问题是当我断开可穿戴设备与调试器或手机的连接时(再也看不到 logcat)。服务停止工作!并且该服务不会再触发片段 activity。任何帮助都可以救命。提前致谢
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:
...
}
}
}
}
抱歉回答晚了。 所以解决方案实际上只不过是第二个 wakelock() 。当我找到解决方案时,我已经击败了自己一个星期(在我自己的脑海中)。这是一个愚蠢的错误,但当它被解决时,真是太好了:)
所以 :
@Override
public void onCreate() {
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "systemService");
}
和 onStartCommand(..)
只需添加:
mWakelock.acquire();
所以最后有两个唤醒锁 希望对您有所帮助!