Android 6 Error in Reading Device ID? [closed]

2019-08-29 12:58发布

问题:

While Reading Device ID etc. I am getting an error in Android 6.

How ever its working fine in all other versions 4.0,5.1 etc..

This is my code:

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));
    }

}

this is error I am getting

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.

How ever I Have given permission in manifest.. but I need to give Run time permissions in Android 6...

For that I followed this https://developer.android.com/training/permissions/requesting.html

But I tried but its notworking..

Can any one suggest me how to use Run time permissions in my code..

回答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;
            }
        }
    }


回答2:

Android sdk has implemented Runtime permission feature from version 6.0 onwards. No matter weather you have given permission in manifest file you need to check that permission before you access.

You can check at runtime using either hasSystemFeature(PackageManager.FEATURE_TELEPHONY) or getPhoneType().

Or you can check by following format I have done for Camera permission

 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;
    }
}