android - 运行时权限第二次不起作用

标签 android android-permissions

我能够构建具有运行时权限的应用程序。在第一次出现启动画面时,它会显示应用程序第二次点击启动画面时的启动画面。这是我的代码。我点击了此链接。

https://developer.android.com/training/permissions/requesting.html

我做到了。但只有 1 次它从第二次卡在启动画面时运行完美。

if (android.os.Build.VERSION.SDK_INT >= 23)
        {
            // Here, thisActivity is the current activity
              if (ContextCompat.checkSelfPermission(SplashScreen.this,
                              Manifest.permission.READ_PHONE_STATE)
                      != PackageManager.PERMISSION_GRANTED) {

                  // Should we show an explanation?
                  if (ActivityCompat.shouldShowRequestPermissionRationale(SplashScreen.this,
                          Manifest.permission.READ_PHONE_STATE)) {

                      // 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(SplashScreen.this,
                              new String[]{Manifest.permission.READ_PHONE_STATE},
                              MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

                      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                      // app-defined int constant. The callback method gets the
                      // result of the request.
                  }
              }
        }


 @Override
      public void onRequestPermissionsResult(int requestCode,
              String permissions[], int[] grantResults) {

          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length > 0
                      && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                          Intent i = new Intent(SplashScreen.this, EmtyActivity.class);
                          startActivity(i);

                          // close this activity
                          finish();


                  } else {
                    // Contact permissions have not been granted yet. Request them directly.
                      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                              MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

                  }
                  return;
              }

              // other 'case' lines to check for other
              // permissions this app might request
          }
      }

最佳答案

如果已经授予权限,则需要一个 else 语句来执行操作:

if (ContextCompat.checkSelfPermission(SplashScreen.this,
                Manifest.permission.READ_PHONE_STATE)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(SplashScreen.this,
            Manifest.permission.READ_PHONE_STATE)) {

        // 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(SplashScreen.this,
                new String[]{Manifest.permission.READ_PHONE_STATE},
                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}
else
{
            Intent i = new Intent(SplashScreen.this, EmtyActivity.class);
            startActivity(i);

            // close this activity
            finish();

}

关于android - 运行时权限第二次不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37097222/

相关文章:

android - 运行即时应用程序时,相机权限始终被拒绝,Google Pixel 上没有显示对话框

Android:捕获和存储图片

android - Kotlin 中的 Dagger 2 Unresolved reference DaggerXXXSub

android拖放ImageView onTouchListener

android - GrantPermissionCallable : Permission: android. permission.SET_TIME 不能被授予

SplashScreen 上的 Android Marshmallow 运行时权限

android - 如何指定android自定义颜色资源: day/night/high contrast

android - 无法将标题 View 添加到列表 - 已调用 setAdapter

android - 我可以使用与随附证书不同的证书重新签署 .apk 吗?

java - 从 Android fragment 调用时权限被拒绝。使用权限已设置