Don't Be negative Don't Be negative - 6 months ago 67
Android Question

Android 6 Error in Reading Device ID? To Give Permission In My code

While Reading Device ID etc.. I am getting Error in Android 6
How ever its working fine in all other versions..

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..

Please suggest me for my code... I am unable to understand...
Please...

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