2

更新:我收到了这个错误,就像在这个问题(12)中解释的那样,但是根据这个答案,这只是一个内部日志,不应该对功能产生影响

V/GoogleSignatureVerifier:com.google.android.gms 签名无效

原始问题:

即使应用程序被杀死,我也必须实现与用户位置相关的不同功能。为此,我正在使用一项服务。我在尝试跟踪用户时遇到了问题,因为我在请求位置更新时似乎遇到了问题。

这是我的服务中最相关的部分:

免责声明:为了阅读,代码已被简化。与服务绑定相关的所有方法以及几个回调已被删除,因为不是问题的一部分

public class LocationService extends RoboService implements LocationListener {

    private static final String TAG = "LocationService";
    private static final long UPDATE_INTERVAL_IN_MILLISECONDS = TimeUnit.SECONDS.toMillis(5);
    private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = TimeUnit.SECONDS.toMillis(1);
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

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

        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .build();

            mGoogleApiClient.connect();
        }

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_REDELIVER_INTENT;
    }

    public Location getCurrentLocation() {
        ConnectionResult connectionResult = mGoogleApiClient.blockingConnect();
        if (connectionResult.isSuccess()) {
            return LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        }
        return null;
    }

    public void startLocationUpdates() {
        ConnectionResult connectionResult = mGoogleApiClient.blockingConnect();
        if (connectionResult.isSuccess()) {
            Log.d(TAG, "startLocationUpdates: Posting request");
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            Log.d(TAG, "startLocationUpdates: Request posted");
        }
    }

    @Override
    public void onLocationChanged(Location newLocation) {
        System.out.println("LocationService.onLocationChanged");
    }
}

我正在使用mGoogleApiClient.blockingConnect(),因为由于架构限制,我无法使用连接回调,但是当我尝试获取当前位置时我没有任何问题。

startLocationUpdates()我请求位置更新时,执行似乎丢失了,也Log.d(TAG, "startLocationUpdates: Request posted")没有onLocationChanged(Location newLocation)被调用。

这是日志猫:

05-23 11:42:47.509 13547-13547/com.example.location D/MapPresenterImpl: onRouteAdded: Starting route tracking
05-23 11:42:47.512 13547-13950/com.example.location D/LocationService: startLocationUpdates: Posting request
05-23 11:43:28.115 13547-13557/com.example.location W/art: Suspending all threads took: 7.530ms

我的代码基本上是从Android 示例中复制而来的。我下载了示例源代码,我让它正常工作。我的代码和示例之间的两个唯一区别是我在服务内部和 using 中mGoogleApiClient.blockingConnect(),但我不知道这会如何影响这种方式。

有人遇到过这种行为吗?我真的迷路了,我会感谢任何帮助。

解决方法:

最后,我使用 PendingIntent 而不是 Callback 方法解决了这个问题。我仍然很高兴知道为什么会发生这种情况,但是如果有人遇到这个问题并且不知道如何继续,这是我的解决方法。

开始位置更新的方法:

public void startLocationTracking(long routeId) {
    ConnectionResult connectionResult = mGoogleApiClient.blockingConnect();
    if (connectionResult.isSuccess()) {
        Intent intent = new Intent(this, TrackingService.class);
        PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        LocationRequest request = new LocationRequest();
        request.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        request.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, pi);
    }
}

跟踪服务:

public class TrackingService extends IntentService {

    public TrackingService() {
        super("TrackingService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (LocationResult.hasResult(intent)) {
            LocationResult locationResult = LocationResult.extractResult(intent);
            Location location = locationResult.getLastLocation();
            // Do whatever you need with the location
        }
    }
}

使用这种方法我遇到了另一个奇怪的问题。如果您在用于构建 PendingIntent 的 Intent 中添加了一些额外内容,则在调用 TrackingService 时,Intent 额外内容中没有任何位置,只有在创建 Intent 时添加的内容。我的解决方法是使用 SharedPreferences 来传递我需要的 id,但我并不真正相信这个补丁。这个问题在这个问题中讨论。

4

0 回答 0