有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从未调用java Android LocationListener onLocationChanged()

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

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

这是我的代码:

package com.trackit.app.locationupdatetest;

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

import com.google.安卓.gms.common.ConnectionResult;
import com.google.安卓.gms.common.api.GoogleApiClient;
import com.google.安卓.gms.location.LocationListener;
import com.google.安卓.gms.location.LocationRequest;
import com.google.安卓.gms.location.LocationServices;
import com.google.安卓.gms.maps.CameraUpdateFactory;
import com.google.安卓.gms.maps.GoogleMap;
import com.google.安卓.gms.maps.OnMapReadyCallback;
import com.google.安卓.gms.maps.SupportMapFragment;
import com.google.安卓.gms.maps.model.LatLng;
import com.google.安卓.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, 安卓.Manifest.permission.
            ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, 安卓.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[]{安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
        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 安卓:name="安卓.permission.INTERNET" />
        <uses-permission 安卓:name="安卓.permission.ACCESS_FINE_LOCATION" />
        <uses-permission 安卓:name="安卓.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE" />
        <uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE" />

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

        <application
            安卓:allowBackup="true"
            安卓:icon="@mipmap/ic_launcher"
            安卓:label="@string/app_name"
            安卓:supportsRtl="true"
            安卓: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
                安卓:name="com.google.安卓.geo.API_KEY"
                安卓:value="@string/google_maps_key" />

            <activity
                安卓:name=".MapsActivity"
                安卓:label="@string/title_activity_maps">
                <intent-filter>
                    <action 安卓:name="安卓.intent.action.MAIN" />

                    <category 安卓:name="安卓.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

格拉德尔

apply plugin: 'com.安卓.application'

安卓 {
    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-安卓.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.安卓.support:appcompat-v7:24.2.0'
    compile 'com.google.安卓.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 安卓 google client api

onLocationChanged isn't being called

有人能指出我的错误吗=S


共 (1) 个答案

  1. # 1 楼答案

    这里有另一个答案

    要想调用OnLocation Changed,有一行代码至关重要:

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

    在你的例子中,这段代码深深地存在于你的代码中,需要满足许多条件才能达到这一点:

    您需要的条件:

    • 未引发安全异常
    • 最后位置==null
    • (grantResults.length>;0&;grantResults[0]==已授予PackageManager.PERMISSION)
    • !!(ContextCompat.checkSelfPermission(这个,android.Manifest.permission.ACCESS_FINE_LOCATION)!=包装经理。许可(已授予)
    • 按下按钮
    我会考虑在你的代码中放更多的日志,以确定这些条件实际上是被满足的…追溯到:

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

    PS-我看到你在使用Android N权限模型。我还没有把我的脚趾放在水里,因为它似乎制造了混乱。。。我刚刚设定了目标SDK=22.:)