阅读设备ID时出现Android 6错误?

时间:2016-05-25 06:56:34

标签: android permissions manifest

在阅读设备ID等时,我在Android 6中收到错误。

在所有其他版本4.0,5.1等中如何正常工作。

这是我的代码:

public class MyDevIDS extends AppCompatActivity {

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

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = pInfo.versionName;
        TextView versionText = (TextView) findViewById(R.id.tv7);
        versionText.setText("Version  : " + version);

        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String imei_id = telephonyManager.getDeviceId();
        TextView imei_idT = (TextView) findViewById(R.id.tv9);
        String imei_idV = "<b><font color=#008000>" + imei_id + "</b></font>";
        imei_idT.setText(Html.fromHtml("IMEI ID  : " + imei_idV));

        String device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        TextView device_idT = (TextView) findViewById(R.id.tv10);
        String device_idV = "<b><font color=#008000>" + device_id + "</b></font>";
        device_idT.setText(Html.fromHtml("Device ID  : " + device_idV));
    }

}

这是我得到的错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.devid/com.test.devid.MyDevIDS}: java.lang.SecurityException: getDeviceId: Neither user 10102 nor current process has android.permission.READ_PHONE_STATE.

我如何在清单中获得许可..但我需要在Android 6中提供运行时权限...

为此,我跟着this https://developer.android.com/training/permissions/requesting.html

但是我试过但它没有工作......

任何人都可以建议我如何在我的代码中使用运行时权限..

2 个答案:

答案 0 :(得分:1)

public class MyDevIDS extends AppCompatActivity {

        private static final int REQUEST_READ_PERMISSION = 123;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

            if (CheckPermission(this, Manifest.permission.READ_PHONE_STATE)) {
                YourStuffHandling();
            } else {
                RequestPermission(MyDevIDS.this, Manifest.permission.READ_PHONE_STATE, REQUEST_READ_PERMISSION );
            }
        }


        private void YourStuffHandling() {
            PackageInfo pInfo = null;
            try {
                pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            String version = pInfo.versionName;
            TextView versionText = (TextView) findViewById(R.id.tv7);
            versionText.setText("Version  : " + version);

            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String imei_id = telephonyManager.getDeviceId();
            TextView imei_idT = (TextView) findViewById(R.id.tv9);
            String imei_idV = "<b><font color=#008000>" + imei_id + "</b></font>";
            imei_idT.setText(Html.fromHtml("IMEI ID  : " + imei_idV));

            String device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
            TextView device_idT = (TextView) findViewById(R.id.tv10);
            String device_idV = "<b><font color=#008000>" + device_id + "</b></font>";
            device_idT.setText(Html.fromHtml("Device ID  : " + device_idV));
        }

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

            switch (permsRequestCode) {

                case REQUEST_READ_PERMISSION: {
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        YourStuffHandling();
                    } else {
                        ShowToast(getString(R.string.permission_needed_sms));
                    }
                    return;
                }
            }
        }

        public void RequestPermission(Activity thisActivity, String Permission, int Code) {
            if (ContextCompat.checkSelfPermission(thisActivity,
                    Permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                        Permission)) {
                } else {
                    ActivityCompat.requestPermissions(thisActivity,
                            new String[]{Permission},
                            Code);
                }
            }
        }

        public boolean CheckPermission(Context context, String Permission) {
            if (ContextCompat.checkSelfPermission(context,
                    Permission) == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        }
    }

答案 1 :(得分:0)

Android sdk已经从6.0版本开始实施了运行时权限功能。无论您在清单文件中获得许可,您都需要在访问之前检查该权限。

您可以使用hasSystemFeature(PackageManager.FEATURE_TELEPHONY)或getPhoneType()在运行时进行检查。

或者您可以按照我为相机许可

所做的格式进行检查
 public void getCameraPermission(){
    if (!checkPermission()) {
        requestPermission();
    }
}

private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);

    if (result == PackageManager.PERMISSION_GRANTED){
        return true;
    } else {
        return false;
    }
}
private void requestPermission(){

    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)){

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
    } else {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
    }
}

 @Override
 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              Toast.makeText(MainActivity.this,"Permission granted",Toast.LENGTH_SHORT).show();
                //store permission in shared pref

            }

            else {
                Toast.makeText(MainActivity.this,"Permission denied",Toast.LENGTH_SHORT).show();
                //store permission in shared pref
            }
            break;
    }
}