在前台服务中注册和注销步数传感器

时间:2019-04-18 11:38:03

标签: java android performance sensormanager

我有一个服务,它是前台服务,我想在sleepTime时使用unregisterListener(),在weakUpTime时使用registerListener。 因此,我使用了处理程序发布延迟,但由于postdelay()的时间较长,因此有时无法使用。

这是我的源代码:onCreate()服务,registerAgain()unRegisterAgain()

    Calendar calendar = Calendar.getInstance();
    long currentTime = calendar.getTimeInMillis();

    calendar.set(Calendar.HOUR_OF_DAY, HOUR_TIME_SLEEP);
    calendar.set(Calendar.MINUTE, MINUTE_TIME_SLEEP);

    long sleepTime = calendar.getTimeInMillis();
    Log.d("hung.nv14", "Sleep Time  " + calendar.get(Calendar.HOUR_OF_DAY));

    calendar.set(Calendar.HOUR_OF_DAY, HOUR_TIME_WEAK_UP);
    calendar.set(Calendar.MINUTE, MINUTE_TIME_WEAK_UP);

    long weakUpTime = calendar.getTimeInMillis();
    Log.d("hung.nv14", "Weak Up time  " + calendar.get(Calendar.HOUR_OF_DAY));
    Log.d("hung.nv14", currentTime + " _ " + sleepTime + " - " + weakUpTime);

    if (currentTime >= weakUpTime && currentTime <= sleepTime) {
        unRegisterAgain();
    } else {
        Log.d(TAG, "onCreate unregisterListener");
        mSensorManager.unregisterListener(this);
        sleeping();
        registerAgain();
    }

    private void registerAgain () {

        Log.d(TAG, "registerAgain");

        Calendar calendar = Calendar.getInstance();

        long currentTime = calendar.getTimeInMillis();

        calendar.set(Calendar.HOUR_OF_DAY, HOUR_TIME_WEAK_UP);
        calendar.set(Calendar.MINUTE, MINUTE_TIME_WEAK_UP);
        calendar.set(Calendar.SECOND, 0);

        long weakUpTime = calendar.getTimeInMillis();

        if (currentTime > weakUpTime) {
            calendar.add(Calendar.DATE, 1);
            weakUpTime = calendar.getTimeInMillis();
        }

        long h = (weakUpTime - currentTime) / 3600 / 1000;
        long m = ((weakUpTime - currentTime) - h * 3600 * 1000) / 60 / 1000;

        Log.d(TAG, "Register after " + h + "h " + m + " m");

        mHandler.postDelayed(() -> {
            Log.d(TAG, "registerAgain" + "currentTime < sleepEnd");
            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
            unRegisterAgain();
        }, weakUpTime - currentTime);
    }

    private void registerAgain () {

        Log.d(TAG, "registerAgain");

        Calendar calendar = Calendar.getInstance();

        long currentTime = calendar.getTimeInMillis();

        calendar.set(Calendar.HOUR_OF_DAY, HOUR_TIME_WEAK_UP);
        calendar.set(Calendar.MINUTE, MINUTE_TIME_WEAK_UP);
        calendar.set(Calendar.SECOND, 0);

        long weakUpTime = calendar.getTimeInMillis();

        if (currentTime > weakUpTime) {
            calendar.add(Calendar.DATE, 1);
            weakUpTime = calendar.getTimeInMillis();
        }

        long h = (weakUpTime - currentTime) / 3600 / 1000;
        long m = ((weakUpTime - currentTime) - h * 3600 * 1000) / 60 / 1000;
        Log.d(TAG, "Register after " + h + "h " + m + " m");

        mHandler.postDelayed(() -> {
            Log.d(TAG, "registerAgain" + "currentTime < sleepEnd");
            mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
            unRegisterAgain();
        }, weakUpTime - currentTime);
    }

请帮助我!谢谢〜

0 个答案:

没有答案