android 6.0中的android运行时权限?

时间:2016-02-03 06:58:11

标签: android android-6.0-marshmallow android-permissions

对于Android 6.0版本,我为android 6.0添加了运行时权限。但如果用户拒绝该权限意味着我的应用程序崩溃。我在下面附上了我的代码。所以任何人都帮助我。提前谢谢。

我的代码:

 private static final String[] INITIAL_PERMS={
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_CONTACTS,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.READ_PHONE_STATE,

            Manifest.permission.READ_CALL_LOG,Manifest.permission.WRITE_CALL_LOG,Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_CONTACTS, Manifest.permission.SEND_SMS,Manifest.permission.RECEIVE_SMS,

            Manifest.permission.CAMERA,Manifest.permission.READ_SMS
    };
 if(Build.VERSION.SDK_INT >= 23 ){


            if (!canAccessLocation() || !canAccessLocation_1() || !canAccessPhone() || !canAccessContacts() || !canAccessStorage() || !canAccessContacts1()
                    || !canAccessSms() || !canAccessSms1() || !canAccessSms2() || !canAccessCamera()) {
                ActivityCompat.requestPermissions(SplashActivity.this, INITIAL_PERMS, INITIAL_REQUEST);
            }
        }
        else if (Build.VERSION.SDK_INT < 23) {


            //not a marshamallow
        }

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


        switch(requestCode) {
            case REQUEST_CAMERA:
                if (canAccessCamera()) {
                    doCameraThing();
                }
                else {
                    bzzzt();
                }
                break;

            case REQUEST_CONTACTS:
                if (canAccessContacts()&&canAccessContacts1()) {
                    doContactsThing();
                }
                else {
                    bzzzt();
                }
                break;

            case REQUEST_LOCATION:
                if (canAccessLocation()&&canAccessLocation_1()) {
                    doLocationThing();
                }
                else {
                    bzzzt();
                }
                break;
            case REQUEST_PHONE:
                if (canAccessPhone()) {
                    dophoneThing();
                }
                else {
                    bzzzt();
                }
                break;
            case REQUEST_SMS:
                if (canAccessSms()&&canAccessSms1() && canAccessSms2()) {
                    doSmsThing();
                }
                else {
                    bzzzt();
                }
                break;
            case REQUEST_STORAGE:
                if (canAccessStorage()) {
                    dostorageThing();
                }
                else {
                    bzzzt();
                }
                break;
        }
        checking();

    }

//location

    private boolean canAccessLocation() {
        return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
    }
    private boolean canAccessLocation_1() {
        return(hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION));
    }



    //phone
    private boolean canAccessPhone() {
        return(hasPermission(Manifest.permission.READ_PHONE_STATE));
    }
    private boolean canAccessStorage() {
        return(hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE));
    }


    //sms
    private boolean canAccessSms() {
        return(hasPermission(Manifest.permission.READ_SMS));
    }
    private boolean canAccessSms1() {
        return(hasPermission(Manifest.permission.SEND_SMS));
    }
    private boolean canAccessSms2() {
        return(hasPermission(Manifest.permission.RECEIVE_SMS));
    }


    //camera
    private boolean canAccessCamera() {
        return(hasPermission(Manifest.permission.CAMERA));
    }

    //contact

    private boolean canAccessContacts() {
        return(hasPermission(Manifest.permission.READ_CONTACTS));
    }
    private boolean canAccessContacts1() {
        return(hasPermission(Manifest.permission.WRITE_CONTACTS));
    }

    private boolean hasPermission(String perm) {
        return(ContextCompat.checkSelfPermission(this, perm)==
                PackageManager.PERMISSION_GRANTED);
    }

    private void bzzzt() {
        Toast.makeText(this, R.string.toast_bzzzt, Toast.LENGTH_LONG).show();
    }

    private void doCameraThing() {
        Toast.makeText(this, R.string.toast_camera, Toast.LENGTH_SHORT).show();
    }

    private void doContactsThing() {
        Toast.makeText(this, R.string.toast_contacts, Toast.LENGTH_SHORT).show();
    }

    private void doLocationThing() {
        Toast.makeText(this, R.string.toast_location, Toast.LENGTH_SHORT).show();
    }
    private void doSmsThing() {
        Toast.makeText(this, R.string.toast_sms, Toast.LENGTH_SHORT).show();
    }

    private void dostorageThing() {
        Toast.makeText(this, R.string.toast_storage, Toast.LENGTH_SHORT).show();
    }

    private void dophoneThing() {
        Toast.makeText(this, R.string.toast_phone, Toast.LENGTH_SHORT).show();
    }


    //checking

    public void checking(){

        if (!m_sharedPreference.contains("useruid") && flag == true  ) {

            db.ClearTables();
            executeCircleAndOperatorsList();
        }
        else if (m_sharedPreference.contains("useruid") && flag == true  && CHECKDB == false) {

            db.ClearTables();
            executeCircleAndOperatorsList();
        }else if (m_sharedPreference.contains("useruid") && flag == true && CHECKDB == true) {

            servicemodifyremainder();
        } else if (m_sharedPreference.contains("useruid") && CHECKDB == true && flag == false) {

            Intent intent1 = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent1);
        }
    }

2 个答案:

答案 0 :(得分:0)

您最好的选择是捕获请求权限的输出。您可以通过检查grantResults参数获取已授予结果的数量来执行此操作。

如果您没有获得足够的金额,您可以选择以下两个选项之一(从用户体验的角度来看):

  • 再次询问用户权限
  • 显示一条消息,说明用户无权启动应用程序。

(显然,这两种情况不必相互排斥)。

你可以通过以下方式实现这一点:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // 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.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

可以在Android docs for Requesting Permissions at Runtime找到更多信息。

答案 1 :(得分:0)

    int permissions_All = 1;
    String[] Permissions = {Manifest.permission.RECEIVE_SMS, 
    Manifest.permission.READ_SMS 
     };
    if (!hasPermissions(this, Permissions)) {
        ActivityCompat.requestPermissions(this, Permissions, permissions_All);
    }
}

public static boolean hasPermissions(Context context, String... Permissions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Permissions != null) {
        for (String Permission : Permissions) {
            if (ActivityCompat.checkSelfPermission(context, Permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }

在活动的onCreate方法中写下所有这些代码 提及清单文件中的所有权限,并在字符串数组中提及它们