TimerTask Android 蓝牙

发布于 2024-11-19 04:57:11 字数 1020 浏览 6 评论 0原文

我正在实现一个在 Android 启动时启动的服务,它应该每 10 分钟扫描一次附近的蓝牙设备。当它发现设备时,它会执行一些工作。此外,这种定期扫描应该在设备开启的整个过程中进行。我正在尝试安排一个 TimerTask,但我不明白如何使用它。我想它应该启动这个服务并让该服务完成工作而不是在TimerTask的run方法中编写代码?我如何从 TimerTask 启动服务,因为这似乎是解决我的问题的最简单方法,但 TimerTask 是 java.util 的一部分,而不是 Android 的类之一。

我刚刚找到了Android的AlarmManager。我应该用那个吗?它可以启动一个服务吗?


到目前为止我已经有了这个,但我需要帮助:

class Timer extends Service
{
    private Handler myHander;

Runnable r = new Runnable()
{
    run()
    {
        startService(new Intent(Timer.this.getApplicationContext() ,MyService.class));

myHandler.postDelayed(r,10 分钟); } }

onCreate()
{   
    myHandler=new MyHandler();

}   

onStartCommand()
{
    //Do the bluetooth work.

r.run(); }

onDestroy() { super.onDestroy(); myHandler.removeCallback(r); }

} MyService 类扩展了 Service { }

抱歉,我不明白这里的格式是如何工作的。

我需要重写服务中的 onDestroy() 吗?在哪里使用 stopService() ?

I am implementing a Service that starts when Android boots, and it's supposed to scan every 10 mins for nearby Bluetooth devices. When it discovers devices, it does some work. Also, this periodic scanning should occur the entire time the device is on. I am trying to schedule a TimerTask, but I don't understand how to use it. I guess it should start this service and let the service do the work instead of writing the code in the TimerTask's run method? How would I start the service from the TimerTask as this seems the easiest way to remedy my problem, but TimerTask is part of java.util and not one of Android's classes.

I just found Android's AlarmManager. Should I use that? Can it start a Service?


So far I have this, but I need help:

class Timer extends Service
{
    private Handler myHander;

Runnable r = new Runnable()
{
    run()
    {
        startService(new Intent(Timer.this.getApplicationContext() ,MyService.class));

myHandler.postDelayed(r,10 minutes);
}
}

onCreate()
{   
    myHandler=new MyHandler();

}   

onStartCommand()
{
    //Do the bluetooth work.

r.run();
}

onDestroy()
{
super.onDestroy();
myHandler.removeCallback(r);
}

}
class MyService extends Service
{
}

Sorry, I don't understand how the formatting works here.

Will I need to override onDestroy() in the Service? Where to do I use stopService() ?

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

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

发布评论

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

评论(1

何以畏孤独 2024-11-26 04:57:12

您需要:

  1. 编写将从您的一项活动和/或设备启动后启动的服务。
  2. onStart/onStartCommand 中的服务中,您需要使用 HandlerAlaramManager 安排定期更新。

在这种情况下,HandlerAlarmManager 之间的区别是:当设备处于深度睡眠时,Handler 将不会运行,而您可以配置< code>AlaramManager 唤醒设备并运行您的代码。

我建议使用 Handler,因为它更容易,而且因为您说您只需要在设备唤醒时运行代码。

还有一件事,Handler 不使用额外的线程,而 TimerTask 则使用额外的线程。在 Android 上,这被认为是一种不好的做法,仅浪费计时器的线程。

有关如何使用 Handler 重复任务的示例代码可以在此处找到:延迟重复任务?

You need to:

  1. Write service that will be started from one of your activities and/or after device has booted.
  2. In your service in onStart/onStartCommand you need to schedule either using Handler or AlaramManager periodic updates.

The difference between Handler and AlarmManager in this case will be that: Handler will not run when device is in deep sleep, while you can configure AlaramManager to wake up the device and run your code.

I'd recommend to go with Handler, as its easier and because you said you only need to run your code when device is awake.

And one more thing, Handler doesn't use extra thread while TimerTask does. And this is considered a bad practice on Android to waste on thread for timer only.

An example code for how to repeat task using Handler can be found here: Repeat a task with a time delay?.

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