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