2016-03-30 16 views
0

私はAndroid-Mランタイムアクセス許可に取り組んでいました。私は自分のフラグメントの1つにLocationパーミッションを追加したかったのです。ここに私のコードです。 Androidは許可を求めていますが、権限を拒否しても何も起こりません。ダイアログボックスを閉じますが、それでもactivateGPS()が機能し、値を返します。前もって感謝します。Android Marshmallowランタイムアクセス許可 - 動作していません

if (Build.VERSION.SDK_INT >= 23) { 
     // Marshmallow+ 
     getPermissionToReadUserContacts(); 
    } else { 
     // Pre-Marshmallow 
     System.out.println("CODMOB: Device not marshmallow"); 
     activateGPS(); 
    } 


    activateGPS(); 

    return view; 
} 
private static final int LOCATION_PERMISSIONS_REQUEST = 1; 
public void getPermissionToReadUserContacts() { 
    System.out.println("CODMOB: getPermissionToReadUserContacts()"); 
    // 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid 
    // checking the build version since Context.checkSelfPermission(...) is only available 
    // in Marshmallow 
    // 2) Always check for permission (even if permission has already been granted) 
    // since the user can revoke permissions at any time through Settings 

     if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 

      // The permission is NOT already granted. 
      // Check if the user has been asked about this permission already and denied 
      // it. If so, we want to give more explanation about why the permission is needed. 
      System.out.println("CODMOB: The permission is NOT already granted."); 
      if (shouldShowRequestPermissionRationale(
        Manifest.permission.ACCESS_COARSE_LOCATION)) { 
       // Show our own UI to explain to the user why we need to read the contacts 
       // before actually requesting the permission and showing the default UI 
       System.out.println("CODMOB: We need to access location."); 
      }else{ 

      // Fire off an async request to actually get the permission 
      // This will show the standard permission request dialog UI 
      requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
        LOCATION_PERMISSIONS_REQUEST); 

      } 
     } 
    } 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    // Make sure it's our original READ_CONTACTS request 
    if (requestCode == LOCATION_PERMISSIONS_REQUEST) { 
     if (grantResults.length>0 && 
       grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      System.out.println("CODMOB:permission granted"); 
      activateGPS(); 
      //Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show(); 
     } else { 
      System.out.println("CODMOB: permission denied"); 
      //Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 

答えて

1
もあなたの活動に

使用コード上記

@Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.containerView); 
     if (fragment != null) { 
      fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); 
     } 
    } 
+0

申し訳ありません。それは私のところからの誤りでした。それでもコードがonRequestPermissionResult()の内部に到達していないことがわかりました。そうではなく、許可を拒否すると、logcatに「CODMOB:permission denied」というメッセージが表示されるはずです。 –

+0

コンテナアクティビティ –

+0

にonRequestPermissionResult()を追加しましたが、2回目にアプリケーションを開くと、権限ダイアログも表示されません。つまり、requestPermissionメソッドが実行されていないと思います。 –

0
ActivityCompat.requestPermissions(MainActivity.this, 
       new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},  100); 

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

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

      // permission was granted, yay! Do the 
      // contacts-related task you need to do.   
     } else { 

      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
      Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
     } 
     return; 
     } 

    // other 'case' lines to check for other 
    // permissions this app might request 
    } 
} 
0
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) != 
      PackageManager.PERMISSION_GRANTED) { 
     final String[] permission = new String[]{ 
       android.Manifest.permission.WRITE_EXTERNAL_STORAGE, 
       android.Manifest.permission.READ_EXTERNAL_STORAGE, 
       android.Manifest.permission.INTERNET, 
       android.Manifest.permission.READ_PHONE_STATE, 
       android.Manifest.permission.PACKAGE_USAGE_STATS, 
       android.Manifest.permission.ACCESS_NETWORK_STATE, 
       Manifest.permission.ACCESS_COARSE_LOCATION, 
       Manifest.permission.ACCESS_FINE_LOCATION, 
     }; 
     ActivityCompat.requestPermissions(this, permission, 0); 
    } 
をこのメソッドを追加します....における-もかかわらず、フラグメントの

if (Build.VERSION.SDK_INT >= 23) { 
     // Marshmallow+ 
     getPermissionToReadUserContacts(); 
    } else { 
     // Pre-Marshmallow 
     System.out.println("CODMOB: Device not marshmallow"); 
     activateGPS(); 
    } 
    return view; 
} 

にコードを変更

あなたの活動に役立つかもしれません。

関連する問題