Android AlarmManager 用于定期读取传感器

发布于 2024-10-05 04:12:07 字数 1423 浏览 9 评论 0原文

我的任务是定期读取后端的手机传感器(例如 WiFi、加速计)。

我当前的解决方案是使用 AlarmManager。

具体来说,我们有:

在“主”程序(一个活动)中,我们使用 PendingIntent.getService:

public class Main extends Activity {
...
Intent intent = new Intent(this, AutoLogging.class);
mAlarmSender = PendingIntent.getService(this, 0, intent, 0);
am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, 0, 5*1000, mAlarmSender);
}

在“AutoLogging”程序(一个服务)中,我们定期响应警报:

public class AutoLogging extends Service {
...
@Override
public void onCreate() {
   Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
   super.onDestroy();
   Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onUnbind(Intent intent) {
   Toast.makeText(this, "onUnbind", Toast.LENGTH_SHORT).show()
   return super.onUnbind(intent);
}

@Override
public void onStart(Intent intent, int startId) {
   super.onStart(intent, startId);
   Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
   // Read sensor data here
}

@Override
   public IBinder onBind(Intent intent) {
   Toast.makeText(this, "onBind", Toast.LENGTH_SHORT).show();
   return null;
}
}

我的问题是:

当我使用这个警报服务时,每个警报仅调用 OnCreate 和 OnStart。

我的问题是:

(1)我们是否需要调用OnDestroy(或onBind、onUnbind)?

(2) 这是使用 AlarmManager 的正确方法吗(与“广播接收器”相比)?

谢谢! 文森特

I have a task to periodically read the phone sensors (e.g. WiFi, accelerometer) in the backend.

My current solution is to use an AlarmManager.

Specifically, we have:

In the "main" program (an activity), we use PendingIntent.getService:

public class Main extends Activity {
...
Intent intent = new Intent(this, AutoLogging.class);
mAlarmSender = PendingIntent.getService(this, 0, intent, 0);
am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, 0, 5*1000, mAlarmSender);
}

In the "AutoLogging" program (a service), we respond to the alarm periodically:

public class AutoLogging extends Service {
...
@Override
public void onCreate() {
   Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
   super.onDestroy();
   Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
}

@Override
public boolean onUnbind(Intent intent) {
   Toast.makeText(this, "onUnbind", Toast.LENGTH_SHORT).show()
   return super.onUnbind(intent);
}

@Override
public void onStart(Intent intent, int startId) {
   super.onStart(intent, startId);
   Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
   // Read sensor data here
}

@Override
   public IBinder onBind(Intent intent) {
   Toast.makeText(this, "onBind", Toast.LENGTH_SHORT).show();
   return null;
}
}

My problem is:

When I use this alarm service, only OnCreate and OnStart are called at each alarm.

My questions are:

(1) Do we need to call OnDestroy (or onBind, onUnbind)?

(2) Is this a correct way to use AlarmManager (compared with "broadcase receiver")?

Thanks!
Vincent

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

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

发布评论

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

评论(3

雨巷深深 2024-10-12 04:12:08

对于任何偶然发现这一点的人来说,晚了几年。

关于服务时调用哪种方法,请参阅此处的这篇文章:

Android onCreate 或用于启动服务的 onStartCommand

您希望在 onStartCommand 中触发。

Chiming in years late for anyone that stumbles upon this.

In terms of which method gets called when for services see this post here:

Android onCreate or onStartCommand for starting service

You'd want to trigger in the onStartCommand.

回眸一遍 2024-10-12 04:12:07

AlarmManager 仅使用挂起的意图并执行意图操作,即在您的情况下启动服务。警报到期时使用 onCreate 创建服务(如果尚未运行),然后通过调用 onStart 启动。读取完传感器数据后,您可以使用 stopSelf() 停止服务,该服务最终将调用 onDestroy()。您不应在服务中显式调用 onDestroy()、onBind() 或 onUnBind()。

如果您将广播接收器与警报管理器一起使用,则必须在接收器的 onReceive 中启动此服务。在这种情况下,使用服务对我来说似乎是合适的。

AlarmManager just uses the pending intent and performs the intent action, i.e starting service in your case.On alarm expiry service is created using onCreate( if it is not already running ) and then started by calling onStart. After you finish reading the sensor data, you can stop the service using stopSelf() which will ultimately call onDestroy().You shouldn't call onDestroy(),onBind() or onUnBind() explicitly in the service.

If you use broadcast receiver with alarm manager you have to start this service in onReceive of receiver.Using Service seems appropriate to me in this case.

当爱已成负担 2024-10-12 04:12:07

如果您想在 Android 中定期安排作业而不是使用警报管理器,您可以使用 GCM 网络管理器执行定期任务。这在内部使用警报管理器或作业调度程序,具体取决于 Android 版本。通过更灵活的选项,它也更容易使用。
这篇文章很棒——
https://www.bignerdranch.com /blog/optimize-battery-life-with-androids-gcm-network-manager/

If you want to schedule a job in android periodically instead of using an alarm manager you can use GCM network manager with the periodic task. This internally uses an alarm manager or job scheduler depending on the Android version. It is also easier to use with a more flexible option.
This article is great -
https://www.bignerdranch.com/blog/optimize-battery-life-with-androids-gcm-network-manager/

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