Android权限6.0及以上

时间:2017-12-04 17:14:48

标签: android android-permissions

我正在尝试为Marshmallow创建我自己的检查请求权限类,并知道有正常和危险的权限。该应用程序的基本功能是阻止来电。

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在阻止呼叫中的参与是什么?

1 个答案:

答案 0 :(得分:0)

代码下方结帐:

import PropTypes from 'prop-types';
import React from 'react';
import MyComponent1 from './MyComponent1.jsx'
import MyComponent2 from './MyComponent2.jsx'

export default class KneatModalContent extends React.Component {

   constructor() {
      super();
      this.components = [MyComponent1, MyComponent2];
   }

   render() {
     return (
     <div className='modal-contents'>
        {this.components.map(function (component, i) {
           return <{ component } key= { i } />
        })}
     </div>
 )
}

}

以下代码适用于您的情况,您可以使用以下任何方法检查或 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()