java - onLocationChanged() 方法未被调用

标签 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

旁注:我确实尝试添加另一个 requestLocationUpdates() 实现,并将字符串提供程序作为 LocationManager.GPS_PROVIDER 但出现 fatal error 导致我的应用程序崩溃

PS:我完整的调试logcat:

    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

最佳答案

为什么onLocationChanged的原因没有被调用,因为你没有声明权限 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> .

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

<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"/>

关于java - onLocationChanged() 方法未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712224/

相关文章:

java - 无法在 Java 中发送正确的 POST 请求 (Android Studio)

android - react-native, "abortOnError false",但由于 lint 错误,android 构建失败

xml - 如何限制XQuery结果中的返回顺序?

java - 分布式Web应用系统的安全性

java - 最小化窗口时自动重绘

java - 如何在相机捕获的图像上叠加图像或 View

android - 在 TabHost 内更改 Activity 转换

android - PopupWindow.getContentView().getLayoutParams() 在 Android Marshmallow 中返回一个不同的类

Android 设置 - 反向依赖

python - findall 或 finditer 可以在这个 python ElementTree 示例中使用吗?