屏幕关闭时,Android背景位置更新未到来

时间:2016-09-06 08:01:08

标签: android location background-service fusedlocationproviderapi android-fusedlocation

我们正在开发移动跟踪应用。对于位置更新,Fused location API以高精度作为优先级使用。

即使屏幕关闭,也需要更新位置。所以,我们使用的是背景Service。后台Service也获取部分WakeLock,以便设备不会进入休眠状态。 在后台Service,我们使用Service的待定更新请求位置更新。

问题是我们只在屏幕打开时接收位置更新。一旦屏幕关闭,位置更新就会停止。此外,Thread运行Service,任何时候都不会被杀死。

当屏幕关闭BroadcastReceiver时再次创建位置请求也不起作用。

以下是背景Service类(RouteExecution):

private static final String TAG = "RouteExecution";

private static RouteExecution routeExecution = null;

private static GoogleApiClient mGoogleApiClient;
private static PendingIntent pendingIntent = null;
private PowerManager.WakeLock waitLock;
/**
 * Creates an IntentService.  Invoked by your subclass's constructor.
 */
public RouteExecution() {
    super(TAG);

    RouteExecution.routeExecution = this;
}

@Override
public void onCreate() {
    super.onCreate();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mGoogleApiClient.connect();

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new PowerButtonReceiver();
    registerReceiver(mReceiver, filter);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    waitLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    waitLock.acquire();
}

@Override
protected void onHandleIntent(Intent intent) {

    if (LocationResult.hasResult(intent)) {

        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();

        GPSLocation gpsLocation = new GPSLocation(location);

        Log.d(TAG, "Location Accuracy: " + location.getAccuracy() + " "
                + " has: " + location.hasAccuracy() + " Provider: " + location.getProvider()
                + " long: " + location.getLongitude() + " lat: " + location.getLatitude());
    }
}



public boolean checkLocationPermission() {

    if (ActivityCompat.checkSelfPermission(routeExecution, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(routeExecution, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return false;
    }

    return true;
}

public void createLocationListener() {

    if (!this.checkLocationPermission()) {
        return;
    }

    LocationRequest mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(5000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setSmallestDisplacement(0);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    PendingResult<Status> statusPendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
            mLocationRequest, pendingIntent);
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    Log.d(TAG, mGoogleApiClient.isConnected() + "   On Connected");
    synchronized (this) {

            createLocationListener();
    }
}


public static GoogleApiClient getmGoogleApiClient() {
    return mGoogleApiClient;
}

@Override
public void onDestroy() {
    super.onDestroy();


    waitLock.release();
    mGoogleApiClient.disconnect();
}

public static RouteExecution getRouteExecution() {
    return routeExecution;
}

public static void setPendingIntent(PendingIntent pendingIntent) {
    RouteExecution.pendingIntent = pendingIntent;
}

Service使用AlarmManager开始。这是提取:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent updateServiceIntent = new Intent(context, RouteExecution.class);
PendingIntent pendingUpdateIntent = PendingIntent.getService(context, 0, updateServiceIntent, 0);
RouteExecution.setPendingIntent(pendingUpdateIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, 50000, pendingUpdateIntent);

BroadcastReceiver

public class PowerButtonReceiver extends BroadcastReceiver {
    private static final String TAG = "PowerButton";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d(TAG, "Power Button");
        if (RouteExecution.getRouteExecution() != null) {
            RouteExecution.getRouteExecution().createLocationListener();
        }
    }
}

即使屏幕关闭,如何继续获取位置更新。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先,在WakeLock中查询Service根本不可靠。该代码执行之前,设备可以重新进入休眠状态。

我会拨打PendingIntent.getBroadcast()而不是PendingIntent.getService(),因为WakeLock在技术上保证onReceive(),并从那里开始Service

WakeLock中获取onReceive(),在适当时启动Service并从WakeLock发布Service

使用基本相同的WakefulBroadcastReceiver,而不必自己实现大部分逻辑。

另一个可能的问题是在Marshmallow设备上使用AlarmManager.set()

如果Doze处于活动状态,则不会在这些设备上触发此方法设置的警报。

在API级别23 +上使用setAndAllowWhileIdle()代替set()