其他分享
首页 > 其他分享> > Android 6.0.1中的位置返回0.0,但在4.0.1中有效

Android 6.0.1中的位置返回0.0,但在4.0.1中有效

作者:互联网

这个问题已经在这里有了答案:            >            How to request Location Permission at runtime                                    4个
我一直在寻找解决方案,并尝试了一些可以找到的相关解决方案,但仍然无法解决.代码的特定部分未运行.我遇到的问题是,这一切都可以在我的旧NEC Terrain(4.0.1,API 15)上运行,但是不能在我的新Blackberry Priv(6.0.1,API 23)上运行.在较新的电话中,第一个错误日志“ Net enabled”显示在logcat中,然后其他任何错误日志均不显示(我将它们用于故障排除).在旧手机中,一切正常,并显示所有日志.什么阻止了此代码的运行?我添加了用于添加权限的代码,这些代码已在下面发布.我的清单中还包含以下内容:

<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" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>

这是locationManager的声明:

locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

这是代码:

if (isNetworkEnabled) {
                Log.e("Tag", "Net Enabled");

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Log.e("Tag", "No permission");
                }
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager == null) {
                    Log.e("Tag", "It's null.");
                }
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    Log.e("Tag", "NetLocMan OK");

                    if (location != null) {
                        Log.e("Tag", "NetLoc OK");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.e("Tag", "" + latitude);
                        Log.e("Tag", "" + longitude);
                    }
                }

            }

            if (isGPSEnabled) {
                Log.e("Tag", "Location" + location);
                if (location == null) {
                    Log.e("Tag", "GPSLoc finding");
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.e("Tag", "Location" + location);

                    if (locationManager != null) {
                        Log.e("Tag", "GPSLocMan OK");
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        Log.e("Tag", "Location" + location);

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.e("Tag", "" + latitude);
                            Log.e("Tag", "" + longitude);
                        }
                    }
                }
            }

这是我正在使用的权限代码:

private static final int PERMS_REQUEST_CODE = 123;

if (!hasPermissions()){
            requestPerms();
        }

private boolean hasPermissions(){
        int res = 0;
        //string array of permissions,
        String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

        for (String perms : permissions){
            res = checkCallingOrSelfPermission(perms);
            if (!(res == PackageManager.PERMISSION_GRANTED)){
                return false;
            }
        }
        return true;
    }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean allowed = true;

        switch (requestCode){
            case PERMS_REQUEST_CODE:

                for (int res : grantResults){
                    // if user granted all permissions.
                    allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
                }

                break;
            default:
                // if user not granted permissions.
                allowed = false;
                break;
        }

        if (allowed){
            //user granted all permissions we can perform our task.

        }
        else {
            // we will give warning to user that they haven't granted permissions.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){
                    Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
                }
                if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)){
                    Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

谢谢你的帮助!如果您需要更多代码,请告诉我.

解决方法:

从Android 6.0(API级别23)开始,Google要求开发人员在运行时请求潜在的危险权限:

ActivityCompat.requestPermissions(MainMenu.this, 
        Manifest.permission.ACCESS_FINE_LOCATION, 
        GET_PERMISSIONS_REQUEST_CODE
);

这将显示一个弹出窗口,要求获得访问用户位置的权限.您事先提出的​​任何位置请求都将为空.

标签:locationmanager,android-6-0-marshmallow,android
来源: https://codeday.me/bug/20191111/2021861.html