请求位置更新在设备睡眠期间不起作用

时间:2016-07-30 12:56:23

标签: 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

2 个答案:

答案 0 :(得分:3)

请注意,在移动设备上持续使用纯GPS作为位置提供商非常耗能。说完之后,我会按如下方式执行你的任务:

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

答案 1 :(得分:0)

AFAIK,wake lock是最简单的方法。 PowerManager.WakeLock

  

唤醒锁定是一种机制,用于指示您的应用程序需要让设备继续运行。

任何使用WakeLock的应用程序都必须在应用程序清单的元素中请求android.permission.WAKE_LOCK权限。通过致电newWakeLock(int, String)获取唤醒锁。

调用acquire()获取唤醒锁定并强制设备保持在创建唤醒锁定时请求的级别。

你应该获得一个唤醒锁:

//in onCreate of your service
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"gps_service");
cpuWakeLock.acquire();

// Release in onDestroy of your service
if (cpuWakeLock.isHeld())
cpuWakeLock.release();
and add this to your manifest:

https://developer.android.com/reference/android/Manifest.permission.html 如果您需要连续的位置更新,则上述情况很好。