Android GPS 跟踪和 WakeLock

发布于 2024-12-09 13:23:23 字数 1614 浏览 0 评论 0原文

我需要在 Android 手机上进行后台 GPS 跟踪,目前我计划有两种情况:

  1. 当手机连接到(汽车)充电器时,获取频繁的 GPS 位置(每分钟)。
  2. 当手机未连接充电器时,仅每 5-30 分钟轮询一次位置,具体取决于电池受影响的程度。

我知道我需要在服务中实现跟踪,并且可能会显示一条通知以保持服务处于活动状态。不过,我推测手机可能随时进入睡眠模式,这会停止 GPS 跟踪。

对于这些场景,在手机不进入睡眠状态且不会消耗太多电量的情况下进行连续后台 GPS 跟踪的最佳方法是什么?

到目前为止,我收集了以下信息:

对于场景 1,我'你可能需要获取一个 WakeLock,并保持 CPU 始终处于唤醒状态(但屏幕可能会休眠)。 MyTracks 应用程序似乎在 TrackRecordingService. OpenGPSTracking 似乎可以 相同

对于场景 2,我可以使用 AlarmManager 安排每 x 分钟检查一次。我的服务需要获取临时 WakeLock,等待单个 GPS 位置,然后停止并再次释放 WakeLock。

这两种方法(永久 WakeLock 与 AlarmManager)的功耗如何比较?如果仅每 30 分钟检查一次,GPS 能否在合理的时间内接收到准确的位置?

StackOverflow 上有一些相关问题,但没有一个有令人满意的答案。一些半有用的:

I need to do background GPS tracking on an Android phone, and I'm currently planning on having two scenarios:

  1. When the phone is connected to a (car) charger, get frequent GPS locations (every minute).
  2. When the phone is not connected to a charger, only poll for a location once every 5-30 minutes, depending on how much the battery is affected.

I know that I'll need to implement the tracking in a service, and probably show a notification to keep the service alive. However, I gather that the phone might go into sleep mode at any time, which would stop the GPS tracking.

For these scenarios, what is the best way to do continuous background GPS tracking, without the phone going to sleep, and without draining too much power?

So far I gathered the following:

For scenario 1, I'll probably need a acquire a WakeLock, and keep the CPU awake all the time (but the screen may sleep). The MyTracks application seems to be doing this in TrackRecordingService. OpenGPSTracking seems to do the same.

For scenario 2, I can use AlarmManager to schedule a check every x minutes. My service will need to acquire a temporary WakeLock, wait for a single GPS location, then stop and release the WakeLock again.

How would the power usage compare for these two methods (permanent WakeLock vs AlarmManager)? Can the GPS receive an accurate location in a reasonable amount of time when only checking every 30 minutes?

There are a few related questions on StackOverflow, but none with satisfying answers. Some semi-useful ones:

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

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

发布评论

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

评论(2

美羊羊 2024-12-16 13:23:23

无论电源是否连接,您都可以使用广播接收器并执行相应的操作。

电源连接时:

public class PowerConnected extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_CONNECTED")){
      //Initialize your listener here...
   }
   }

}

在清单文件中:

<receiver android:name=".receiver.PowerConnected" >
<intent-filter >
     <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>

电源断开时:

public class PowerDisconnected extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_DISCONNECTED")){
    //Do your task here...      
  }
 }
}

在清单文件中:

<receiver android:name=".receiver.PowerDisconnected" >
<intent-filter >
   <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

现在您应该使用 service 并在那里初始化您的 GPS 。连接电源后,使用 GPS_Provider,否则使用网络提供商。如果需要,请使用 wakelock

You can use broadcast receiver if the power is connected or not and do your action accordingly.

For Power Connected:

public class PowerConnected extends BroadcastReceiver{
   @Override
   public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_CONNECTED")){
      //Initialize your listener here...
   }
   }

}

In your manifest file:

<receiver android:name=".receiver.PowerConnected" >
<intent-filter >
     <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>

When Power is Disconnected:

public class PowerDisconnected extends BroadcastReceiver{
  @Override
  public void onReceive(Context context, Intent intent) {
  if(intent.getAction().equalsIgnoreCase("android.intent.action.ACTION_POWER_DISCONNECTED")){
    //Do your task here...      
  }
 }
}

In your manifest file:

<receiver android:name=".receiver.PowerDisconnected" >
<intent-filter >
   <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>

Now you should use service and initialize your GPS there. When power is connected use GPS_Provider else use network provider.Use wakelock if needed.

飞烟轻若梦 2024-12-16 13:23:23
  1. 当手机连接到充电器时,您可以获取唤醒锁,因为您打算获取频繁的 GPS 位置,在这种情况下,AlarmManager 似乎不是一个好主意。

  2. 当手机未连接到充电器时,您可以使用警报管理器,因为您只会每 5-30 分钟轮询一次位置。获取唤醒锁将显着影响您的电池寿命。

实施取决于你。

干杯....!!

  1. When the phone is connected to the charger u can acquire the wakelock as u a planning to get frequent GPS locations, AlarmManager doesnt seems to be an great idea in this case.

  2. When phone is not connected to the charger u may use alarm manager as u ll only poll for a location once every 5-30 minutes. Acquiring wakelock will affect ur battery life significantly.

Implementation is up to u.

Cheers....!!

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