Android TelephonyManager doesn't work with android studio simulator? -
i using simple code retreive phone device data, reason time open app on emulator, pops message saying "unfortunately *app name* has closed"
, shuts down app.
this code using:
telephonymanager tm=(telephonymanager)getsystemservice(context.telephony_service); string imeinumber=tm.getdeviceid();
i wrote code test telephonymanager
methods nothing works except for: "getphonetype();"
any ideas what's problem? maybe because runing on android studio emulator?
the problem says: getdeviceid: neither user 10058 nor current process has android.permission.read_phone_state
though added permission in manifest:
<uses-permission android:name="android.permission.read_phone_state" />
(above application part)
thank help
are running android m
? if , because it's not enough declare permissions
in manifest
. permissions
, have explicitly ask user in run-time
:
check link run time permission
.
code snippet.
define
globally
inactivity
.
private static final int permission_request_code = 1;
do stuff in
oncreate
.
// here, thisactivity current activity if (contextcompat.checkselfpermission(testingactivity.this, manifest.permission.read_phone_state) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(testingactivity.this, manifest.permission.read_phone_state)) { // show expanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. } else { // no explanation needed, can request permission. activitycompat.requestpermissions(testingactivity.this, new string[]{manifest.permission.read_phone_state}, permission_request_code); // my_permissions_request_read_contacts // app-defined int constant. callback method gets // result of request. } }
finally
overrride onrequestpermissionsresult
.
@override public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) { switch (requestcode) { case permission_request_code: { // if request cancelled, result arrays empty. if (grantresults.length > 0 && grantresults[0] == packagemanager.permission_granted) { // permission granted, yay! } else { // permission denied, boo! disable // functionality depends on permission. } return; } // other 'case' lines check other // permissions app might request } }
Comments
Post a Comment