0

您好,我目前正在尝试使用 GoogleApiClient 获取我的 Android 设备的纬度和高度值。我一直在关注这里的指南:

GoogleApiClient:最后已知位置指南

我也试图从我的服务而不是活动中做到这一点,这是我的代码片段供参考:

public class AppService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient googleApiClient;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    googleApiClient.connect();
    try {
        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        Log.d("location", String.valueOf(lastLocation.getLatitude()));
        Log.d("location", String.valueOf(lastLocation.getLongitude()));
    } catch (SecurityException e) {
        Log.e("Location error", "Location object is null.");
    }

    googleApiClient.disconnect();

    //Stop service once it finishes its task
    stopSelf();
    return Service.START_STICKY;
}

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

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

每次我尝试访问 Location 变量以获取纬度和高度时,我都会收到 NullPointerException,知道为什么吗?

还想包含我的 AndroidManifest 文件以防万一,据我所知,我拥有所需的权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="app"
      android:versionCode="1"
      android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </activity>
    <receiver
            android:name=".AutoStart"
            android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
    <service android:name=".AppService"/>
</application>
</manifest>
4

1 回答 1

1

根据文档,调用googleApiClient.connect()是异步的:

公共抽象无效连接()

将客户端连接到 Google Play 服务。此方法立即返回,并在后台连接到服务。如果连接成功,则调用 onConnected(Bundle) 并执行排队的项目。失败时,将调用 onConnectionFailed(ConnectionResult)。

如果客户端已经连接或正在连接,则此方法不执行任何操作。

因此,您应该等待onConnected回调被执行,并将您的位置检索代码放在那里。

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
  if (googleApiClient == null) {
     googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
  }
  googleApiClient.connect();
  return Service.START_STICKY;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
  try {
    Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    Log.d("location", String.valueOf(lastLocation.getLatitude()));
    Log.d("location", String.valueOf(lastLocation.getLongitude()));
  } catch (SecurityException e) {
    Log.e("Location error", "Location object is null.");
  }

  googleApiClient.disconnect();
  stopSelf();
}
于 2016-09-26T20:24:06.690 回答