서비스에서 TTS를 사용하여 특정 값을 측정 할 때 가속도 센서를 모니터링하고 뭔가를 알려주는 프로그램을 만들었습니다. 장치가 켜져있을 때 모든 것이 잘 작동하지만 때로는 잠자기 상태로 전환 될 때가 있습니다. 때로는 잠시 후 작동하기도합니다. 때로는 장치를 깨우기 위해 전원 단추를 누르는 것처럼 작동하기도하고 때로는 작동하지 않는 경우도 있습니다. 어떤 생각이 있습니까?장치가 잠자기 상태 일 때 TTS가 작동하지 않습니다.
public class MyService extends Service implements SensorEventListener, OnInitListener {
private SensorManager SenMan;
private Sensor AccSen;
private TextToSpeech mTTS = null;
private PowerManager pm;
private WakeLock wl;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
SenMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
AccSen = SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SenMan.registerListener(this, AccSen, SensorManager.SENSOR_DELAY_NORMAL);
pm = (PowerManager)getSystemService(POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
}
@Override
public void onDestroy() {
SenMan.unregisterListener(this);
super.onDestroy();
}
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {}
@Override
public final void onSensorChanged(SensorEvent event) {
double a = Math.sqrt(Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2));
if (Math.abs(a - 9.8)/0.98 > 25) Say();
}
private void Say() {
wl.acquire();
mTTS = new TextToSpeech(this, this);
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
mTTS.speak("Text to say", TextToSpeech.QUEUE_FLUSH, null);
wl.release();
}
}
물론 배터리를 제거하거나 기기를 비트로 박살 내고 싶지는 않습니다. 이것은 안드로이드의 가장 큰 문제 중 하나입니다. 다른 사람들도 같은 문제를 겪고있는 것을 보았습니다. 사용자가 스마트 폰에서 수행하기를 원하는 많은 작업에서 장치가 잠자기 상태 일 때 센서를 모니터링하고 가치에 따라 행동합니다. – Vahid