Request Permission at Run time

When i was upgrading from api 19 Android KitKat, one of an error i got is “Exception java.lang.SecurityException: getDeviceId: Neither user 10129 nor current process has android.permission.READ_PHONE_STATE”. Here is what i trace and fix following up these steps from site¬†

Reason: in old api lower than 23, they accept app to set some dangerous permission inside AndroidManifest.xml of android project<uses-permission android:name="android.permission.READ_PHONE_STATE"/>, but now it’s not affect on new one and don’t allow that line then it become useless.

Solution: if you are writing for and admin app, not a social app. You can define setup processing before rolling out the app to user, just go to Settings->Applications->Choose the app and give it all permissions.

For social app: we need to add these line of code inside the Activity working on.

Step 1: Check permission, show the popup request if not. Remember add

int constant REQUEST_READ_PHONE_STATE so that we can reuse this flag in request callback.

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
        } else {

Step 2: Handle call back when you request permission inside the Activity

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
            if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {


This entry was posted in Android and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s