12

If the device is running Android 6.0 or higher when im trying to get phone number using getLine1Number():

java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10184 nor current process has android.permission.READ_PHONE_STATE. This is coming out.

I declared permission as :

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Sanket Prabhu
  • 2,232
  • 3
  • 20
  • 33

2 Answers2

21

In Android 6.0, you need to explicitly ask the user to grant the permissions. Just declaring it in the manifest isn't enough.

This article in the docs is a great place to start learning the new model, but I'll give a brief summary.

Every time you perform an action that requires a "dangerous permission," you need to check if the permission is currently granted, because the user can revoke it at any time.

This can be done with the checkSelfPermission method.

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_PHONE_STATE)
    != PackageManager.PERMISSION_GRANTED) {
        // We do not have this permission. Let's ask the user
}

You can request the permission with the requestPermissions method, as such

ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);

Where PERMISSION_READ_STATE is a constant integer defined by you to check in the callback method later.

You will then override onRequestPermissionsResult in your activity and see if the permission was granted. If it was, you can go ahead and preform the dangerous action.

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_READ_STATE: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permission granted!
                // you may now do the action that requires this permission
            } else {
                // permission denied
            }
            return;
        }

    }
}
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • Sir, Is it possible without disturbing user? – Sanket Prabhu Feb 10 '16 at 15:59
  • 1
    Nope, from 6.0 onward we have to ask the user directly. If you're more curious about why this is the direction Android is going, you might watch this video from Google: https://www.youtube.com/watch?v=f17qe9vZ8RM – Andrew Brooke Feb 10 '16 at 16:00
  • Is there a list of permissions that needs to be asked explicitly? – JiTHiN Mar 29 '16 at 03:35
  • Any permissions that are considered "dangerous". See here: https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous – Andrew Brooke Mar 29 '16 at 03:38
4
public class MainActivity extends AppCompatActivity {

    TextView textView;
    String device_unique_id,IMEI;


    private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;


   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);
    }
    public void GetImei(View view)
    {



        loadIMEI();

    }

    public void loadIMEI() {
        // Check if the READ_PHONE_STATE permission is already available.
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_PHONE_STATE)) {
//                get_imei_data();
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                        MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
            }
        } else {

            TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            IMEI = mngr.getDeviceId();
            device_unique_id = Settings.Secure.getString(this.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
            textView.setText(device_unique_id+"----"+mngr.getDeviceId());
            // READ_PHONE_STATE permission is already been granted.
            Toast.makeText(this,"Alredy granted",Toast.LENGTH_SHORT).show();
        }
    }
       @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) {

            if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

//                Toast.makeText(this,"Alredy DONE",Toast.LENGTH_SHORT).show();
                TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                IMEI = mngr.getDeviceId();
                device_unique_id = Settings.Secure.getString(this.getContentResolver(),Settings.Secure.ANDROID_ID);
                textView.setText(device_unique_id+"----"+mngr.getDeviceId());

            } else {
                Toast.makeText(this,"ehgehfg",Toast.LENGTH_SHORT).show();
            }
        }
    }
Rajasimman R
  • 496
  • 4
  • 14