dani jinji dani jinji - 2 years ago 186
Android Question

Android TelephonyManager doesn't work with android studio simulator?

I am using this simple code to retreive some phone device data, but for some reason any time I open the app on the emulator, it pops up a message saying

"Unfortunately *APP NAME* has closed"
and then it shuts down the app.

this is the code I am using:

TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String IMEINumber=tm.getDeviceId();

I wrote that code just to test the
methods but nothing works except for:

any ideas what's the problem? maybe it is because I am runing it on my android studio emulator?

the problem it says:
getDeviceId: Neither user 10058 nor current process has android.permission.READ_PHONE_STATE
though I added a permission in the manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

(above the application part)

thank you for your help

Answer Source

Are you running Android M ? If so , this is because it's not enough to declare permissions in the manifest. For some permissions, you have to explicitly ask user in the run-time:

Check this Link for Run time Permission.

Code Snippet.

Define this Globally in your Activity.

private static final int PERMISSION_REQUEST_CODE = 1;

Do this stuff in onCreate.

       // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(TestingActivity.this,
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(TestingActivity.this,
                    Manifest.permission.READ_PHONE_STATE)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                        new String[]{Manifest.permission.READ_PHONE_STATE},

                // app-defined int constant. The callback method gets the
                // result of the request.

Finally overrride onRequestPermissionsResult.

    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    // permission was granted, yay!

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.

            // other 'case' lines to check for other
            // permissions this app might request
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download