【问题标题】:Remove Run Time Permission for current location in Marshmallow删除棉花糖当前位置的运行时权限
【发布时间】:2016-06-24 05:23:39
【问题描述】:

我正在尝试创建一个应用程序。应用程序每次打开应用程序时都会询问 Marsh Mallow 中的位置访问权限。请建议解决我的问题的最佳流程。使用以下代码-

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
//for marshmallow
    final int LOCATION_PERMISSION_REQUEST_CODE = 100;
     ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION},
            LOCATION_PERMISSION_REQUEST_CODE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mMap.setMyLocationEnabled(true);
    buildGoogleApiClient();
    mGoogleApiClient.connect();

}

谢谢。

【问题讨论】:

  • 您已经授权了吗?如果没有,在执行此类任务之前征求许可是正常的。
  • 它的 bczz 你每次都在征求许可
  • 在询问权限之前检查它是否已经被用户授予然后询问..试试这个库:github.com/UttamPanchasara/RuntimePermission
  • 在我的回答中查看@Uttams 的想法

标签: java android android-6.0-marshmallow


【解决方案1】:

如果没有,请尝试写入 if else,如果未获得许可,请请求它或做你想做的事情。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(checkSelfPermission(Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION},
                            LOCATION_PERMISSION_REQUEST_CODE);
                }
            } else {
               // do your stuff
            }

并且还覆盖 onRequestPermissionsResult 并在您授予权限后应用您的代码。

【讨论】:

    【解决方案2】:

    以下代码检查应用是否具有使用位置的权限,并在必要时请求该权限:

    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {
    
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.ACCESS_FINE_LOCATION)) {
    
            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
    
        } else {
    
            // No explanation needed, we can request the permission.
    
            ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION_CODE);
    
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    else
    {
        // Permission already granted ... This is where you can continue your further business logic...
    }
    

    以下是回调方法:

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted, yay! Do the
                    // Locatio-related task you need to do.
    
                } else {
    
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    详细解释请访问here(Android官方页面)或this(Tutsplus教程)

    【讨论】:

      【解决方案3】:

      请自行反映您的代码。考虑请求和检查权限的顺序。您应该在请求之前检查权限。这可以确保防止询问是否已授予权限...

      查看example provided by Google 并考虑根据您的需要调整此示例:

      // Here, thisActivity is the current activity
      if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)     {
      
          // Should we show an explanation?
          if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) {
      
              // Show an expanation to the user *asynchronously* -- don't block
              // this thread waiting for the user's response! After the user
              // sees the explanation, try again to request the permission.
      
          } else {
      
              // No explanation needed, we can request the permission.
      
              ActivityCompat.requestPermissions(thisActivity,
                  new String[]{Manifest.permission.READ_CONTACTS},
                  MY_PERMISSIONS_REQUEST_READ_CONTACTS);
      
              // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
              // app-defined int constant. The callback method gets the
              // result of the request.
          }
      }
      

      另外你应该重写回调方法onRequestPermissionsResult来处理用户的grantResults

      // Callback with the request from calling requestPermissions(...)
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             @NonNull String permissions[],
                                             @NonNull int[] grantResults) {
          // Make sure it's our original READ_CONTACTS request
          if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
              if (grantResults.length == 1 &&
                      grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
              } else {
                  Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
              }
          } else {
              super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          }
      }
      

      编辑:

      这是你应该改变的代码

      ActivityCompat.requestPermissions(this, new String[]    {android.Manifest.permission.ACCESS_FINE_LOCATION,     android.Manifest.permission.ACCESS_COARSE_LOCATION},
              LOCATION_PERMISSION_REQUEST_CODE);
      
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
      
          return;
      }
      

      更多阅读请看文章Understanding App Permissions

      【讨论】:

      • if else 条件我应该放在哪里
      猜你喜欢
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多