ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-11-18 18:26:38  阅读:193  来源: 互联网

标签:android-permissions android


我能够在运行时许可的情况下构建应用程序.第一次启动屏幕时,从第二次应用程序启动屏幕时开始显示启动屏幕.这是我的代码.我点击了此链接.

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-permissions,android
来源: https://codeday.me/bug/20191118/2030096.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有