android - 请求位置更新在设备 sleep 期间不起作用

标签 android google-maps gps location locationmanager

我正在使用位置管理器类来接收位置更新,我的要求是我必须监听连续的位置更新,但我面临的问题是,一旦断开连接,我不知道如何重新建立 GPS 连接,而且在某些情况下设备一旦设备休眠,我将无法接收任何位置更新,请提供任何解决方案来实现此目的,我们将不胜感激。

public void setup(MainActivity activity) {
        if (!setup) {
            this.activity = activity;
            Intent locationIntent = new Intent(this.activity, LocationIntentService.class);
            mLocationPendingIntent = PendingIntent.getService(this.activity, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Intent detectedIntent = new Intent(this.activity, ActivityDetectionIntentService.class);
            mDetectedActivityPendingIntent = PendingIntent.getService(this.activity, 0, detectedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            googleApiClient = new GoogleApiClient.Builder(activity)
                    .addConnectionCallbacks(activity)
                    .addOnConnectionFailedListener(activity)
                    .addApi(ActivityRecognition.API)
                    .build();
            setup = true;
        }
    }
**LocationIntentService.java**
public class LocationIntentService extends IntentService {


    public LocationIntentService() {
        super("LocationServices");
    }

    public LocationIntentService(String name) {
        super("LocationServices");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            Location location = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
            if (location != null) {
                Intent localIntent = new Intent(HWUtil.LOCAL_RECEIVER);
                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                localIntent.putExtra(HWUtil.LOCATION, location);
                localIntent.putExtra(HWUtil.FROM, HWUtil.LOCATION_SERVICE);
                sendBroadcast(localIntent);
            }
        }
    }

}

我正在将此位置更新发送到 Broadcastrecciver

最佳答案

请注意,连续使用纯 GPS 作为位置提供程序在移动设备上非常耗能。一旦如此,我将按如下方式执行您的任务:

  1. 我会使用(背景)service这将与您的移动应用程序一起使用。即,移动应用程序将开始执行此服务(检查 startForeground() 功能,以便您的服务几乎可以不间断地运行,并在状态栏上包含一个可以链接到您的 Activity 的通知)。
  2. 该服务(或任何内部类)将实现 LocationListener 接口(interface),并且将成为实际对位置进行采样的服务。一旦获得位置,您将对其进行处理(根据您的要求,您可能希望在另一个线程中处理它,因为创建的服务的默认线程与 Activity UI 相同)。
  3. 处理完成后,您将向 Android Activity 传递响应,即,您将调用 Activity 的公共(public)方法,或者使用处理程序等实现更复杂的通信策略。
  4. 关于连续位置采样,我强烈建议您使用 AlarmManager 服务,以便您可以安排下一次读数(您可以以精确的重复间隔进行,请参阅 official developer's documentation)。
  5. 如果(且仅当)位置更新的处理繁重或耗时(例如,您必须将其传输到服务器),您可以获取并保存 WakeLock避免设备进入休眠模式;不要忘记在处理完成后释放 WakeLock,因为它是移动应用中能量消耗的主要原因,因此请务必小心。

希望对你有帮助

关于android - 请求位置更新在设备 sleep 期间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674208/

相关文章:

android - 没有互联网的本地网络多人游戏

Android Button 未填满 RelativeLayout 中的全部空间

android - 旁遮普文字在手机上无法正确显示

c++ - 如何在Qt中使用谷歌地图

c - Haversine 方位角计算的结果不准确

android - 将设备上的数据与服务器同步?

php - 使用 PHP 和 google Maps Api 计算两个邮政编码之间的距离(英国)

Android 应用程序错误 - 您必须在 Google Cloud 项目上启用计费

mobile - WiFi接入点的位置可以用以下数据进行三角测量吗?

ios - 是否可以为 ios 应用程序开发转弯导航?