当器件为睡眠TTS不起作用器件、睡眠、不起作用、TTS

2023-09-07 03:40:23 作者:都别理俄

我犯了一个程序时一定值是在服务中使用TTS测量,监视加速度传感器,并说一些东西。当设备上的一切工作正常,但是当它有时睡它工作正常,有时经过一段时间的工作,有时它的工作原理就像我preSS的电源按钮来唤醒器件,有时不工作在所有。有没有什么想法?

 公共类的MyService扩展服务实现SensorEventListener,OnInitListener {    私人的SensorManager SenMan;    私人传感器AccSen;    私人文字转语音MTTS = NULL;    私人PowerManager的时;    私人激活锁定WL;    @覆盖    公众的IBinder onBind(意向为arg0){        返回null;    }    @覆盖    公共无效的onCreate(){        super.onCreate();        SenMan =(的SensorManager)getSystemService(Context.SENSOR_SERVICE);        AccSen = SenMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        SenMan.registerListener(这一点,AccSen,SensorManager.SENSOR_DELAY_NORMAL);        PM =(电源管理)getSystemService(POWER_SERVICE);        WL = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,TAG);    }    @覆盖    公共无效的onDestroy(){        SenMan.unregisterListener(本);        super.onDestroy();    }    @覆盖    公众最终无效onAccuracyChanged(传感器传感器,精度INT){}    @覆盖    公众最终无效onSensorChanged(SensorEvent事件){        双一=的Math.sqrt(Math.pow(event.values​​ [0],2)+ Math.pow(event.values​​ [1],2)+ Math.pow(event.values​​ [2],2));        如果(Math.abs(一 -  9.8)/ 0.98→25)说();    }    私人无效说(){        wl.acquire();        MTTS =新的文字转语音(这一点,这一点);    }    @覆盖    公共无效的OnInit(INT状态){        如果(状态== TextToSpeech.SUCCESS)            mTTS.speak(文字说,TextToSpeech.QUEUE_FLUSH,NULL);        wl.release();    }} 

解决方案

TTS不会当设备处于休眠状态的工作。 TTS也当设备关闭将无法正常工作,不得不将电池取下,或已被摔成用大锤位。 TTS的将会的工作时,该设备是清醒的,或者是因为你唤醒它的电源按钮,还是其他什么东西唤醒设备(例如, AlarmManager ,电话呼入)。

传感器可以或可以不当设备处于睡眠状态,这取决于操作系统版本和设备制造商的工作

因此​​,要么你将需要使用激活锁定来保持设备清醒(以显著电池的成本),或者你将不得不拿出另一个应用程序的想法。

I made a program that monitors acceleration sensor and says something when a certain value is measured using TTS in a service. Everything works fine when the device is on, but when it goes to sleep sometimes it works correctly, sometimes it works after a while, sometimes it works just as I press the power button to wake the device and sometimes it does not works at all. Is there any idea?

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();
    }
}
常用按钮开关知识大全

解决方案

TTS will not work when the device is asleep. TTS also will not work when the device is powered off, has had the battery removed, or has been smashed to bits with a sledgehammer. TTS will work when the device is awake, either because you wake it up with the power button, or something else wakes up the device (e.g., AlarmManager, incoming phone call).

Sensors may or may not work when the device is asleep, depending on OS version and device manufacturer.

Hence, either you are going to need to use a WakeLock to keep the device awake (at significant battery cost), or you are going to have to come up with another app idea.