我正在学习开发一个安卓应用程序,以获取设备位置,遵循Google开发者论坛:@http://developer.android.com/training/location/retrieve-current.html#last-known @http://developer.android.com/training/location/receive-location-updates.html
示例展示了如何使用Google Play服务来实现此目的。
但是,getLastLocation()
函数总是返回null。
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
由于最初的位置可能为空,因此我添加了以下行来接收定期位置更新
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
它抛出一个异常,带有以下消息
客户端必须具有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限才能执行任何位置操作
我已经在清单文件中添加了权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我已经打开了 Google 地图来更新当前位置到 Google Play 服务中,但是问题仍然存在。
我是 Android 开发的新手,在发布这个问题之前,我已经进行了大量搜索,但没有找到适合我的解决方案。
我正在使用 Android Studio 1.3.2
进行开发,测试设备为 Android Marshmallow
。
< p >AndroidManifest.xml< /p >
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="newray.acpsa1">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity中的onCreate函数
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
/* Rest functionality */
}
onConnected函数
@Override
public void onConnected(Bundle bundle) {
try{
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LatLng loc = new LatLng(21.356993, 72.826647);
if (location == null) {
Log.d(TAG,"location is again null");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.d(TAG, "Requested for updates");
}
else {
loc = new LatLng(location.getLatitude(),location.getLongitude());
handleNewLocation(location);
}
} catch (Exception e) {
Log.d(TAG, "Error in onConnected. "+e.getMessage());
}
}
日志:
XXXX: Error in onConnected. Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
build.gradle文件中的依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.android.gms:play-services-location:8.1.0'
}