【问题标题】:Android LocationListener onLocationChanged() is never calledAndroid LocationListener onLocationChanged() 永远不会被调用
【发布时间】:2017-01-20 13:02:39
【问题描述】:

我正在开发一个 Android 应用程序,但我在这方面还是新手。

在我的应用程序中,我使用 FusedLocationApi 来获取最后知道的位置并发出位置更新请求,但我遇到了一个问题,即使在 Android Studio 模拟器和三星 Galaxy S5 上同时激活了位置和 WiFi,我也从来没有得到调用 LocationListener 的 onLocationChanged()。

这是我的代码:

package com.trackit.app.locationupdatetest;

import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

private static final String TAG = MapsActivity.class.getSimpleName();
public static final String STARTING_POSITION = "Rio de Janeiro"; //Application's default position
public static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1; //Fine location permission

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private MapsActivity mActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    (findViewById(R.id.Location)).setEnabled(false);
    (findViewById(R.id.StreetView)).setEnabled(false);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    if (mGoogleApiClient == null)
        buildGoogleAPIClient();

    //Create and configure a Location Request object to used while looking for location updates
    if(mLocationRequest == null)
        buildLocationRequest();

    mActivity = this;

}

private synchronized void buildGoogleAPIClient() {

    //Configuring the Google API client before connect
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

private synchronized void buildLocationRequest() {

    mLocationRequest = LocationRequest.create()
            .setInterval(10000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(1000)
            .setSmallestDisplacement(1);

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

/**
 * This method is called when the Activity is no longer visible to the user.
 */
@Override
protected void onStop() {
    if(mGoogleApiClient.isConnected())
        mGoogleApiClient.disconnect();
    super.onStop();
}

/**    
 * This callback is triggered when the map is ready to be used.    
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

public void buttonPressed(View buttonPressed){        
    displayUserLocation();      
}

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

    (findViewById(R.id.Location)).setEnabled(true);
    (findViewById(R.id.StreetView)).setEnabled(true);

}

@Override
public void onConnectionSuspended(int i) {

}

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

    Log.e(TAG,"onConnectionFailed:"+connectionResult.getErrorCode()+","+connectionResult.getErrorMessage());

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull
int[] grantResults) {      

    //Process the user permission's response
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
            processLocationPermissionResult(grantResults);
            break;
        default:
    }        

}

public void processLocationPermissionResult(@NonNull int grantResults[]) {

    // If request is cancelled, the result arrays are empty.
    if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        displayLocation();
    else {} // permission denied, boo! Disable the functionality that depends on this permission.        

}

public void displayUserLocation(){        

    //Verify if the app have permission to access user's location
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.
            ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.
                permission.ACCESS_FINE_LOCATION)) {

            //Asks the permission to access the user's location
            Toast.makeText(this, "This app needs to access your location. " +
                    "Allow the app to access it?", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    MapsActivity.MY_PERMISSIONS_REQUEST_FINE_LOCATION);

        }

    }   else displayLocation(); //If the permission was granted

}

public void displayLocation() throws SecurityException{

    Location lastLocation = LocationServices.FusedLocationApi.
            getLastLocation(mGoogleApiClient);  

    if(lastLocation != null) {

        Log.e(TAG, "!!!!!!");

        LatLng position = new LatLng(lastLocation.getLatitude(), lastLocation.
                getLongitude());
        mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                MapsActivity.STARTING_POSITION));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(position));

    }   else{

        Log.e(TAG, "??????");
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

    }        

}

@Override
public void onLocationChanged(Location location) {

    if(location.getAccuracy() < 10 && location.getSpeed() < 55.55555555555556){
        Log.e(TAG, "onLocationChanged() started");          

        LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
        mMap.addMarker(new MarkerOptions().position(position).title("Marker in " +
                MapsActivity.STARTING_POSITION));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(position));

        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

        Log.e(TAG, "onLocationChanged() terminated");
    }
    /*else{
        //Continue listening for a more accurate location
    }*/

}
}

下面是我的 AndroidManifest.xml 和我的 gradle:

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.trackit.app.locationupdatetest">

        <!--
             The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
             Google Maps Android API v2, but you must specify either coarse or fine
             location permissions for the 'MyLocation' functionality. 
        -->
        <uses-permission android:name="android.permission.INTERNET" />
        <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.WRITE_EXTERNAL_STORAGE" />

        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">

            <!--
                 The API key for Google Maps-based APIs is defined as a string resource.
                 (See the file "res/values/google_maps_api.xml").
                 Note that the API key is linked to the encryption key used to sign the APK.
                 You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
                 You can define the keys for the debug and release targets in src/debug/ and src/release/. 
            -->
            <meta-data
                android:name="com.google.android.geo.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>

分级

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.trackit.app.locationupdatetest"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
}

这周我搜索了很多,但还没有找到有用的答案=s

这些是我搜索过的一些线程(我不想看起来像个懒惰的人,因为我不是,我只是卡住了)

onLocationChanged does not get called using fusedLocationAPI.requestLocationUpdates

Unable to get location updates

onLocationChanged not called on some devices

on locaton changed never gets called in android google client api

onLocationChanged isn't being called

有人可以指出我的错误吗? =S

【问题讨论】:

  • 查看本教程以了解其完成方式 -- androidhive.info/2015/02/…
  • 会不会是被调用了?你的 '' 'Log.e(TAG, "onLocationChanged() started"); '' 在 onLocationChanged() 内的 IF 语句内...所以问题可能是由于某种原因不满足此条件。我还在看这个...
  • @mike M.,抱歉,我已经更改了帖子
  • @Tasos 这个教程是我第一次读到这个主题的教程之一
  • @Boober Bunz,即使没有那个 if,这个问题也会发生,我可以只留下 if 范围内的内容,并且仍然不调用 onLocationChanged() =S

标签: java android gps


【解决方案1】:

我会冒险回答这个问题...如果它有帮助,那太好了...如果没有,对不起...

我有一个使用 FusedLocationprovider 的类似 Activity。

我不是专家,不能说为什么你的不工作,但我的工作,我注意到以下差异:

您的代码:

private synchronized void buildGoogleAPIClient() {

    //Configuring the Google API client before connect
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

private synchronized void buildLocationRequest() {

    mLocationRequest = LocationRequest.create()
            .setInterval(10000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setFastestInterval(1000)
            .setSmallestDisplacement(1);

}

我的代码:

 protected synchronized void buildGoogleApiClient() {
    this.mGoogleApiClient = new GoogleApiClient.Builder(this).
            addConnectionCallbacks(this).
            addOnConnectionFailedListener(this).
            addApi(LocationServices.API).build();
    createLocationRequest();
}

protected void createLocationRequest() {
    this.mLocationRequest = new LocationRequest();
    this.mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    this.mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    this.mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

区别:

1) 你说:

if(mLocationRequest == null)
        buildLocationRequest();

在您的 onCreate 方法中。这是必要的吗? mLocationRequest 在 onCreate() 中不会一直为空吗?

2) 你的方法是私有的,我的保护

3) 您的 createLocationrequest() 被标记为“已同步”...可能因此无法正常工作?

4) 你使用 setSmallestDisplacement() 方法。我看过帖子说这个方法不能正常工作,导致 onLocationChanged() 没有被调用。

5) 您使用“LocationRequest.create()”而不是“new LocationRequest()”来实例化您的 LocationRequest,这可能是问题所在吗?

这有帮助吗?

【讨论】:

  • 我会尝试这些更改并稍后报告=)
  • 确实,setSmallestDisplacement 出于某种原因弄乱了 onLocationChanged() 调用,一旦我评论那行代码就可以工作,感谢你们的支持! =)
  • ofc 即使在 SO 告诉我“感谢您的反馈!声望低于 15 的人所投的票被记录,但不要更改公开显示的帖子分数”之后,我还是点击了赞成箭头,求你的帮助
【解决方案2】:

这是另一个答案。

有一行代码对于调用 onlocationChanged 至关重要:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

在您的情况下,此代码深深地存在于您的代码中,需要满足许多条件才能达到:

您需要的条件:

  • 没有抛出安全异常
  • 最后一个位置 == null
  • (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  • !(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
  • 按下按钮

我会考虑在您的代码中添加更多日志,以确定这些条件实际上是否得到满足...从以下位置向后追溯:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                mLocationRequest, this);

PS - 我看到您使用的是 Android N 权限模型。由于它似乎造成了混乱,我还没有把我的脚趾放在水里......我只是设置了我的目标 SDK = 22。:)

【讨论】:

  • OK ty 为您提供建议 =)
猜你喜欢
  • 2013-10-12
  • 2013-03-22
  • 2023-03-18
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2013-01-02
  • 2016-09-03
相关资源
最近更新 更多