Android运行时权限始终被拒绝

时间:2016-06-07 12:18:07

标签: android android-permissions

我正在尝试在Android Level-23上阅读短信,但它没有显示运行时权限。有什么问题。

我的代码是

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private static final int PERMISSION_REQUEST_SMS = 0;
    private static final String TAG = MainActivity.class.getName();

    private View mLayout;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main        );
         mLayout = findViewById(R.id.main_layout);

         showAllSMS();
     }

     @Override
     public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
         Log.i(TAG, "onRequestPermission Result = "+grantResults[0]);
         if (requestCode == PERMISSION_REQUEST_SMS) {
             // Request for camera permission.
              if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                 Log.i(TAG, "onRequestPermission Result 1");
                 // Permission has been granted. Start camera preview Activity.
                 Snackbar.make(mLayout, "Camera permission was granted. Starting preview.",
                    Snackbar.LENGTH_SHORT)
                    .show();
                 readSMS();
             } else {
                 // Permission request was denied.
                 Snackbar.make(mLayout, "Camera permission request was denied.",
                    Snackbar.LENGTH_LONG)
                    .show();

             }
          }
         // END_INCLUDE(onRequestPermissionsResult)
     }

     private void showAllSMS() {
         Log.i(TAG, "=================");
         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
            == PackageManager.PERMISSION_GRANTED) {
             Log.i(TAG, "================= 1");
        // Permission is already available, start camera preview
             Snackbar.make(mLayout,
                "Camera permission is available. Starting preview.",
                Snackbar.LENGTH_LONG).show();
              readSMS();
         } else {
             Log.i(TAG, "================= 2");
             // Permission is missing and must be requested.
             requestReadSMSPermission();
         }
         // END_INCLUDE(startCamera)
     }

/**
 * Requests the {@link android.Manifest.permission#CAMERA} permission.
 * If an additional rationale should be displayed, the user has to launch the request from
 * a SnackBar that includes additional information.
 */
      private void requestReadSMSPermission() {
    // Permission has not been granted and must be requested.
         if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                 Manifest.permission.READ_SMS)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the      use of the permission.
        // Display a SnackBar with a button to request the missing permission.
             Snackbar.make(mLayout, "Camera access is required to display the camera preview.",
                     Snackbar.LENGTH_INDEFINITE).setAction("OK", new      View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                // Request the permission
                    ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_SMS},
                        PERMISSION_REQUEST_SMS);
                 }
             }).show();

         } else {
             Snackbar.make(mLayout,
                "Permission is not available. Requesting camera permission.",
                     Snackbar.LENGTH_LONG).show();
        // Request the permission. The result will be received in      onRequestPermissionResult().
             ActivityCompat.requestPermissions(this, new String[]     {Manifest.permission.CAMERA},
                     PERMISSION_REQUEST_SMS);
         }
     }

     private void readSMS() {
         Log.d(TAG, "REad smsm");
    }
}

它始终显示“相机权限请求被拒绝。”。它是从Android示例中复制的。具有相机权限的真实代码可以正常工作。

请解释。

3 个答案:

答案 0 :(得分:1)

您正在尝试获取 Read_SMS 权限的运行时权限,为此您需要更改 requestReadSMSPermission()中的 else 条件。

private void requestReadSMSPermission () {
            // Permission has not been granted and must be requested.
            if() {
//your existing code
                } else {
                Snackbar.make(mLayout,
                        "Permission is not available. Requesting Read SMS.",
                        Snackbar.LENGTH_LONG).show();
                // Request the permission. The result will be received in      onRequestPermissionResult().
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS},
                        PERMISSION_REQUEST_SMS);
            }
        }

答案 1 :(得分:0)

更改此行代码,似乎您正在请求相机权限而不是SMS读取和接收权限,并且它未添加到您的清单中。

 ActivityCompat.requestPermissions(this, new String[]     {Manifest.permission.CAMERA},
                 PERMISSION_REQUEST_SMS);

请清理你的代码,每个人都认为你想用相机。

答案 2 :(得分:0)

仅当设备位于 M或以上时,才会请求以下权限。在较旧的平台上,不会请求这些权限,也不会提供这些权限。

<uses-permission-sdk-m android:name="android.permission.READ_CONTACTS" />
<uses-permission-sdk-m android:name="android.permission.WRITE_CONTACTS" />