我正在开发一个Android应用,但在这一领域我仍然很环保。

在我的应用程序中,我正在使用FusedLocationApi来获取最新的已知位置并发出位置更新请求,但遇到了一个问题,即使在Android Studio仿真器和Samsung Galaxy S5上同时激活了Location和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'
}


这个星期我搜寻了很多东西,但是还没有任何有用的答案

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

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

最佳答案

我会冒险回答这个问题...如果有帮助,很好...如果没有帮助,抱歉...

我有一个使用FusedLocationprovider的类似活动。

我不是专家,不能说您的为什么不工作,但是我的却在工作,而且我注意到以下差异:

您的代码:

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()中是否始终为null?

2)您的方法是私有的,受地雷保护

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

4)您使用setSmallestDisplacement()方法。我看到过一些帖子,指出此方法无法正常工作,并导致无法调用onLocationChanged()。

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

这有帮助吗?

关于java - 永远不会调用Android LocationListener onLocationChanged(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39461363/

10-09 00:06