2016-05-08 7 views
3

実行時のアクセス許可でアプリケーションを構築できました。最初のスプラッシュ画面では、スプラッシュ画面に2回目のスプラッシュ画面が表示されます。これは私のコードです。私はこのリンクに従った。実行時のアクセス許可が2回目に動作しない

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

私はそれを作りました。スプラッシュ画面に2回目から完全に動作するのは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 
      } 
     } 

答えて

3

許可が既に付与されている場合は、物事を行うにはelse文を必要とする:lot.uは私の一日保存

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(); 

} 
+1

TNXを... –

関連する問題