您好,我目前正在尝试使用 GoogleApiClient 获取我的 Android 设备的纬度和高度值。我一直在关注这里的指南:
我也试图从我的服务而不是活动中做到这一点,这是我的代码片段供参考:
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>