onLocationChanged()方法没有被调用

时间:2018-08-06 16:44:13

标签: java android xml

我正在尝试使用LocationManager查找设备的位置;无论我如何尝试,onLocationChanged()方法都不会被调用(尝试使用模拟位置应用更改位置,尝试关闭并在手机上尝试在其上运行该应用等)。这是我的代码和logcat:

LocationManager mlocationManager;
LocationListener mlocationListener;
long MIN_TIME = 5000;
float MIN_DISTANCE = 1000;
final int COARSE_LOCATION_REQUEST_CODE=102;
String LOCATION_PROVIDER = LocationManager.NETWORK_PROVIDER;

我的onResume()方法:

 @Override
protected void onResume() {
    super.onResume();
    Log.d("ClimateControl", "onResume was called");
   getWeatherForCurrentLocation();
}

我的getWeatherForCurrentLocation()方法:

 private void getWeatherForCurrentLocation() {

    mlocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    mlocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("ClimateControl", "onLocationChanged() has been called");
            String longitude = String.valueOf(location.getLongitude());
            String latitude = String.valueOf(location.getLatitude());
            Log.d("ClimateControl", "Latitude: " + latitude + "Longitude: " + longitude);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("ClimateControl", "onStatusChanged() has been called");

        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d("ClimateControl", "onProviderEnabled() has been called");


        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d("ClimateControl", "onProviderDisabled() has been called");

        }
    };
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},COARSE_LOCATION_REQUEST_CODE);
    }
    mlocationManager.requestLocationUpdates(LOCATION_PROVIDER,MIN_TIME,MIN_DISTANCE,mlocationListener);

}

我的onRequestPermissionsResult()方法:

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

    if(requestCode == COARSE_LOCATION_REQUEST_CODE){
        if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
            Log.d("ClimateControl","Request granted");
            getWeatherForCurrentLocation();
        }
        else{
            Log.d("ClimateControl","Request denied");
        }
    }

}

关于ClimateControl标签的日志:

08-06 22:09:42.949 29359-29359/com.example.praty.climatecontrol D/ClimateControl: onResume was called

侧注:我确实尝试使用String提供程序作为requestLocationUpdates()添加另一个LocationManager.GPS_PROVIDER实现,但是出现致命错误,使我的应用崩溃了

PS:我完整的调试日志:

    08-07 16:59:35.229 9417-9417/? I/art: Late-enabling -Xcheck:jni
    08-07 16:59:35.388 9417-9417/? W/System: ClassLoader referenced unknown path: /data/app/com.example.praty.climatecontrol-1/lib/arm64
    08-07 16:59:35.449 9417-9417/com.example.praty.climatecontrol W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    08-07 16:59:35.812 9417-9417/com.example.praty.climatecontrol D/ClimateControl: onResume was called
    08-07 16:59:35.861 9417-9486/com.example.praty.climatecontrol I/Adreno: QUALCOMM build                   : bc01238, I8e5c908169
Build Date                       : 12/06/16
OpenGL ES Shader Compiler Version: XE031.09.00.03
Local Branch                     : 
Remote Branch                    : quic/LA.BR.1.3.6_rb1.10
Remote Branch                    : NONE
Reconstruct Branch               : NOTHING
    08-07 16:59:35.900 9417-9486/com.example.praty.climatecontrol I/OpenGLRenderer: Initialized EGL, version 1.4
    08-07 16:59:35.900 9417-9486/com.example.praty.climatecontrol D/OpenGLRenderer: Swap behavior 1
    08-07 16:59:36.581 9417-9417/com.example.praty.climatecontrol E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    08-07 16:59:38.171 9417-9417/com.example.praty.climatecontrol D/ClimateControl: onResume was called

2 个答案:

答案 0 :(得分:0)

由于尚未声明权限onLocationChanged而未调用<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>的原因。

尝试在清单文件中添加缺少的权限,然后重新测试。应该工作正常。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

答案 1 :(得分:0)

我看到您在何处调用requestLocationUpdates方法的问题。

getWeatherForCurrentLocation方法中取出并在onRequestPermissionsResult中看到为权限授予的权限时调用它。

当然,请确保您具有android.permission.INTERNET, android.permission.ACCESS_COARSE_LOCATION and android.permission.ACCESS_FINE_LOCATION清单中提到的3个权限

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

if(requestCode == COARSE_LOCATION_REQUEST_CODE){
    if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
        Log.d("ClimateControl","Request granted");

        //here you know you have permission and then u can ask location updates, this will trigger locationListener methods on respective time you mentioned with below parameters. 

        mlocationManager.requestLocationUpdates(LOCATION_PROVIDER,MIN_TIME,MIN_DISTANCE,mlocationListener);

    }
    else{
        Log.d("ClimateControl","Request denied");
    }
  }

}

希望有帮助。!!