【问题标题】:setMyLocationEnabled on Marshmallow棉花糖上的 setMyLocationEnabled
【发布时间】:2016-03-10 12:05:01
【问题描述】:

我最近开始编程,我尝试在 android 6.0 Marshmallow 中启用位置按钮来制作基本的地图应用程序。 我想我已经理解了新权限模型的工作原理。当我运行该应用程序时,它要求我授予位置权限,但是当我授予它时,位置按钮不会出现。如果我重新启动应用程序,它已经出现。 我会放上我的 onMapReady 方法的代码:

@Override
public void onMapReady(GoogleMap map) {


    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
       map.setMyLocationEnabled(true);
    } else {
        // Show rationale and request permission.
        Toast toast = Toast.makeText(this, "need permission", Toast.LENGTH_LONG);
        toast.show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 0);
        }

    }
    map.setMyLocationEnabled(true);

}

我尝试在 else 中更改最后一个 setMyLocationEnabled,但没有成功。我知道这是一个有点愚蠢的问题,但我不知道如何解决它。 希望可以有人帮帮我。提前致谢

【问题讨论】:

    标签: google-maps location android-6.0-marshmallow


    【解决方案1】:

    您可以在下面添加代码。

    在早期版本的 Android 中,在安装时,它会向用户显示权限对话框,并且他们被授予应用程序清单文件中定义的权限。

    这在 Marshmallow 中发生了变化。现在,每个应用程序都必须请求用户的许可才能访问它。

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
    {
        System.out.println("CHECK_RUN_TIME_PERMISSION_IF_MARSHMELLOW");
        if(!checkPermission()) {
            requestPermission();
        }else {
            System.out.println("CHECK_RUN_TIME_PERMISSION_IF_MARSHMELLOW++");
        }
    }
    
    private boolean checkPermission(){
        int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (result == PackageManager.PERMISSION_GRANTED){
    
            return true;
    
        } else {
    
            return false;
       }
      }
    
    
    private void requestPermission(){
    
        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)){
    
            Toast.makeText(MainActivity.this,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
    
        } else {
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
        }
      }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    Toast.makeText(MainActivity.this,"Permission Granted, Now you can access location data.",Toast.LENGTH_LONG).show();
    
    
                } else {
    
                    Toast.makeText(MainActivity.this,"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();
    
                }
                break;
        }
    }
    

    【讨论】:

    • 嗨 Gomathi,请添加一些关于这段代码如何解决提问者问题的解释。格式化代码以提高可读性。干杯
    • 例如您的应用程序需要访问位置。在安装时的早期版本的 android 中,它显示权限对话框,并且他们被授予应用程序清单文件中定义的权限。这东西变成了棉花糖。现在每个应用程序都必须获得用户的许可才能访问它
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 2023-03-17
    • 2017-02-02
    • 2016-03-15
    • 1970-01-01
    • 2017-05-12
    • 2019-01-29
    • 1970-01-01
    相关资源
    最近更新 更多