我试图获取用户的当前位置,为此我跟随tutorial。
我在运行Android Lollipop的设备上运行以下代码,因此这里没有运行时权限问题。
这是我的代码(更新1.0 ):
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d("onConnected", "called");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mLastLocation != null) {
currentLatDouble = mLastLocation.getLatitude();
currentLngDouble = mLastLocation.getLongitude();
Log.d("cltLC", String.valueOf(currentLatDouble));
Log.d("clntLC", String.valueOf(currentLngDouble));
}
}
此处onConnected
正在打印出来,但clt
和clnt
不是。
此处的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
此处build.gradle
:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.xxx.aaa"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
maven {
url "https://jitpack.io"
}
}
dexOptions {
javaMaxHeapSize "4g"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.google.android.gms:play-services-location:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.firebase:geofire-android:2.1.0'
}
apply plugin: 'com.google.gms.google-services'
我不希望用户在此阶段打开GPS,并希望在没有GPS的情况下检索当前位置。
我看到这个answer后更新了代码,但如果GPS关闭,则没有任何事情发生!
请告诉我这里的问题是什么。