Android M permission for tested READ_PHONE_STATE (dangerous permissions)

If the device is running Android 6.0 or higher, when I try to get the phone number using getLine1Number () :

java.lang.SecurityException: READ_PHONE_STATE required: neither user 10184 nor the current process have android.permission.READ_PHONE_STATE. It is coming out.

I declared permission as:

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
+6
source share
2 answers

In Android 6.0, you need to explicitly ask the user for permissions. Just declaring it in the manifest is not enough.

This doc article is a great place to start exploring a new model, but I will give a brief summary.

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

This can be done using the checkSelfPermission method.

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

You can request permission using 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 that you defined to check the callback method later.

Then you will override onRequestPermissionsResult in your activity and see if permission has been granted. If that were the case, you could go ahead and take a 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; } } } 
+15
source
 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(); } } } 
+1
source

All Articles