android - 每 10 10 分钟获取一次光照水平

发布于 2024-10-24 04:21:52 字数 1738 浏览 1 评论 0原文

我的应用程序需要在其后台服务中具有更新的光级别值(10-15 分钟的延迟就可以了)(这不是商业应用程序).​​.....

我知道如何使用 SensorEventListener 检索光值并且SensorManager 但在 API 文档中明确指出,如果您在不需要监听器时不取消注册监听器,它将在短短几个小时内耗尽电池电量。

现在我的问题是......我如何使用侦听器和传感器管理器每 10-15 分钟检索一次光照水平值?

我可以使用类似下面的计时器来每 10-15 分钟运行一次此任务吗?

    private TimerTask lightSensorTimer  = new TimerTask() {

    @Override
    public void run() {

        if (sensorManager != null)  {

            Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

            if (lightSensor != null) {

                sensorManager.registerListener(sensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);

                while (newLightLevel == lightLevel) {
                    try {

                        **Thread.sleep(1000);**

                    } catch (InterruptedException e) {

                        Log.e(TAG, "Interrupted exception in lightsensor timer: " + e.getMessage());

                    }
                }
                PhoneEvents.this.lightLevel = PhoneEvents.this.newLightLevel;

                sensorManager.unregisterListener(sensorListener, lightSensor);
            }
        }

    }

};

Sensoreventlistener 仅执行此操作:

    private SensorEventListener sensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {

            PhoneEvents.this.newLightLevel = event.values[0];

        }
    }

    public void onAccuracyChanged(Sensor arg0, int arg1) {

    }

};

我什至不确定是否可以在计时器任务中使用 Thread.sleep。但我已经准备好 TimerTasks 需要快速完成,否则它们会批量化。

对于这样的事情有更清洁的解决方案吗?

谢谢

问候, 安德烈亚斯

My app needs to have an updated light level value (a 10-15 minute delay is fine or something) (this is not a commercial app) within its background service....

I know how to retrieve the light value using a SensorEventListener and the SensorManager but in the API docs it says explicitly that if you don't unregister the listener when you don't need it it will drain your battery in just a few hours.

now my question is....how can I use the listener along with the sensor manager to retrieve a light level value every 10-15 minutes?

Could I use something like the below with a Timer that runs this task every 10-15 minutes?

    private TimerTask lightSensorTimer  = new TimerTask() {

    @Override
    public void run() {

        if (sensorManager != null)  {

            Sensor lightSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

            if (lightSensor != null) {

                sensorManager.registerListener(sensorListener, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);

                while (newLightLevel == lightLevel) {
                    try {

                        **Thread.sleep(1000);**

                    } catch (InterruptedException e) {

                        Log.e(TAG, "Interrupted exception in lightsensor timer: " + e.getMessage());

                    }
                }
                PhoneEvents.this.lightLevel = PhoneEvents.this.newLightLevel;

                sensorManager.unregisterListener(sensorListener, lightSensor);
            }
        }

    }

};

The sensoreventlistener does nothing but this:

    private SensorEventListener sensorListener = new SensorEventListener() {

    public void onSensorChanged(SensorEvent event) {

        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {

            PhoneEvents.this.newLightLevel = event.values[0];

        }
    }

    public void onAccuracyChanged(Sensor arg0, int arg1) {

    }

};

I'm not even sure if you can use Thread.sleep within a timer task. But I have ready that TimerTasks need to finish quickly otherwise they batch up.

Is there a cleaner solution for something like this?

Thanks

Regards,
Andreas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2024-10-31 04:21:52

您应该使用调用 serviceIntent 的警报。

每 15 分钟启动一次警报:

    AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(this, YourService.class);
    PendingIntent pi=PendingIntent.getService(this, 0, i, 0);
    mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

您的服务如下所示:

public class YourServiceextends IntentService {
    public YourService() {
        super("YourService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         //do something
    }
}

You should use an alarm that calls a serviceIntent.

Start the alarm every 15 minutes:

    AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent i=new Intent(this, YourService.class);
    PendingIntent pi=PendingIntent.getService(this, 0, i, 0);
    mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,  SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

and your service looks like this:

public class YourServiceextends IntentService {
    public YourService() {
        super("YourService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         //do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文