2017-12-04 30 views
0

私は自分自身のクラスを作成して、Marshmallowに対する要求の許可を確認しようとしていますが、通常の危険なアクセス許可があることを知っています。アプリの非常に基本的な機能は着信をブロックすることです。Androidのアクセス許可6.0以上

public class RequestPermission { 

    private String[] modifyPhoneStatePermission = {Manifest.permission.MODIFY_PHONE_STATE}; 
    private String[] contactsPermission = {Manifest.permission.READ_CONTACTS}; 
    private String[] phonePermissions = {Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE}; 


    private Context context; 

    public RequestPermission(Context context) 
    { 
     this.context = context; 
    } 

public boolean checkModifyPhoneStatePermission() { 
    try 
    { 
     if (ContextCompat.checkSelfPermission(context, modifyPhoneStatePermission[0]) == PackageManager.PERMISSION_GRANTED) { 

      return true; 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

    public void askModifyPhoneStatePermission() { 
    try 
    { 
     ActivityCompat.requestPermissions((Activity) context, modifyPhoneStatePermission, REQUEST_CODE_PERMISSION); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

} 

ただし、これを別のクラスで試すと、ダイアログボックスが表示されません。なぜこうなった? phonePermissions配列にこれを含める必要がありますか?ブロックしている通話にMODIFY_PHONE_STATEが参加していますか?コードの下

+0

氏ナチョに私の答えの希望を確認することができますそれはあなたにとって有益です。 –

答えて

0

チェックアウト:コードの下

 private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 10001; 

        @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

       if (platform_version >= 23) { 
        boolean permission = checkAndRequestPermissions(); 
        /*if (permission) 
         Toast.makeText(this, "All permissions granted!", Toast.LENGTH_LONG).show();*/ 
       } 

     } 







     private boolean checkAndRequestPermissions() { 
      int modifyPhoneStatePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.MODIFY_PHONE_STATE); 
      int contactsPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS); 
      int phonePermissions = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
      int permissionReadExternal = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); 

      List<String> listPermissionsNeeded = new ArrayList<>(); 
      if (modifyPhoneStatePermission != PackageManager.PERMISSION_GRANTED) { 
       listPermissionsNeeded.add(Manifest.permission.MODIFY_PHONE_STATE); 
      } 
      if (contactsPermission != PackageManager.PERMISSION_GRANTED) { 
       listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS); 
      } 
      if (phonePermissions != PackageManager.PERMISSION_GRANTED) { 
       listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE); 
      } 
if (!listPermissionsNeeded.isEmpty()) { 
      ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS); 
      return false; 
     } 

      return true; 
     } 





      @Override 
     public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { 
      Log.d(PERMISSION_TAG, "Permission callback called..."); 
      switch (requestCode) { 
       case REQUEST_ID_MULTIPLE_PERMISSIONS: { 

        Map<String, Integer> perms = new HashMap<>(); 
        // Initialize the map with both permissions 

        perms.put(Manifest.permission.MODIFY_PHONE_STATE, PackageManager.PERMISSION_GRANTED); 
        perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED); 
        perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED); 

        // Fill with actual results from user 
        if (grantResults.length > 0) { 
         for (int i = 0; i < permissions.length; i++) 
          perms.put(permissions[i], grantResults[i]); 
         // Check for both permissions 
         if (perms.get(Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED && 
           perms.get(Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED && 
           perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && 
           perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { 
          Log.d(PERMISSION_TAG, "permission granted"); 
          // process the normal flow 
          //else any one or both the permissions are not granted 
         } else { 
          Log.d(PERMISSION_TAG, "Some permissions are not granted ask again "); 
          //permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission 
    //      //shouldShowRequestPermissionRationale will return true 
          //show the dialog or snackbar saying its necessary and try again otherwise proceed with setup. 
          if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_NETWORK_STATE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
           showDialogOK("Permission required for this app", 
             new DialogInterface.OnClickListener() { 
              @Override 
              public void onClick(DialogInterface dialog, int which) { 
               switch (which) { 
                case DialogInterface.BUTTON_POSITIVE: 
                 checkAndRequestPermissions(); 
                 break; 
                case DialogInterface.BUTTON_NEGATIVE: 
                 // proceed with logic by disabling the related features or quit the app. 
                 checkAndRequestPermissions(); 
                 break; 
               } 
              } 
             }); 
          } 
          //permission is denied (and never ask again is checked) 
          //shouldShowRequestPermissionRationale will return false 
          else { 
           //proceed with logic by disabling the related features or quit the app. 

          } 
         } 
        } 
       } 
      } 
     } 

はあなたのケースで動作しますが、あなたが望む任意の方法で、以下のチェックを使用するか、onCreate()

if (platform_version >= 23) { 
       boolean permission = checkAndRequestPermissions(); 
       /*if (permission) 
        Toast.makeText(this, "All permissions granted!", Toast.LENGTH_LONG).show();*/ 
      } 
関連する問題