permissions - Android 6 Error in Reading Device ID? -
while reading device id etc. getting error in android 6.
how ever working fine in other versions 4.0,5.1 etc..
this 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 error getting
java.lang.runtimeexception: unable 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 have given permission in manifest.. need give run time permissions in android 6...
for followed this https://developer.android.com/training/permissions/requesting.html
but tried notworking..
can 1 suggest me how use run time permissions in code..
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; } } }
Comments
Post a Comment