Error de permiso de Android 6.0

Me aparece este error:

getDeviceId: Neither user 10111 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1599) at android.os.Parcel.readException(Parcel.java:1552) 

Lo había dado en manifiesto sin embargo. ¿Hay algún cambio con respecto a los dispositivos Android 6.XX Marshmallow ? Necesito dar permiso a READ_PHONE_STATE para obtener el IMEI del dispositivo. Pls. Ayuda.

Sí, los permisos han cambiado en Android M. Ahora se solicitan permisos en tiempo de ejecución en lugar de tiempo de instalación anterior a Android M.

Puede ver los documentos aquí

Esta versión presenta un nuevo modelo de permisos, donde los usuarios ahora pueden administrar directamente los permisos de la aplicación en tiempo de ejecución. Este modelo brinda a los usuarios una mejor visibilidad y control sobre los permisos, a la vez que agiliza la instalación y los procesos de actualización automática para los desarrolladores de aplicaciones. Los usuarios pueden otorgar o revocar permisos de forma individual para las aplicaciones instaladas.

En sus aplicaciones que apuntan a Android 6.0 (API nivel 23) o superior, asegúrese de verificar y solicitar permisos en tiempo de ejecución. Para determinar si su aplicación recibió un permiso, llame al nuevo método checkSelfPermission (). Para solicitar un permiso, llame al nuevo método requestPermissions (). Incluso si su aplicación no está orientada a Android 6.0 (API de nivel 23), debe probar su aplicación en el nuevo modelo de permisos.

Para obtener detalles sobre cómo admitir el nuevo modelo de permisos en su aplicación, consulte Trabajar con permisos del sistema. Para obtener sugerencias sobre cómo evaluar el impacto en su aplicación, consulte las Mejores prácticas de permisos.

Para verificar los permisos, debe marcarlos así, tomados de github

 public class MainActivity extends SampleActivityBase implements ActivityCompat.OnRequestPermissionsResultCallback { public static final String TAG = "MainActivity"; /** * Id to identify a camera permission request. */ private static final int REQUEST_CAMERA = 0; // Whether the Log Fragment is currently shown. private boolean mLogShown; private View mLayout; /** * Called when the 'show camera' button is clicked. * Callback is defined in resource layout definition. */ public void showCamera(View view) { // Check if the Camera permission is already available. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // Camera permission has not been granted. requestCameraPermission(); } else { // Camera permissions is already available, show the camera preview. showCameraPreview(); } } /** * Requests the Camera permission. * If the permission has been denied previously, a SnackBar will prompt the user to grant the * permission, otherwise it is requested directly. */ private void requestCameraPermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // For example if the user has previously denied the permission. Snackbar.make(mLayout, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE) .setAction(R.string.ok, new View.OnClickListener() { @Override public void onClick(View view) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); } }) .show(); } else { // Camera permission has not been granted yet. Request it directly. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA); } } /** * Display the {@link CameraPreviewFragment} in the content area if the required Camera * permission has been granted. */ private void showCameraPreview() { getSupportFragmentManager().beginTransaction() .replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance()) .addToBackStack("contacts") .commit(); } /** * Callback received when a permissions request has been completed. */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == REQUEST_CAMERA) { // Received permission result for camera permission.est."); // Check if the only required permission has been granted if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Camera permission has been granted, preview can be displayed Snackbar.make(mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT).show(); } else { Snackbar.make(mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT).show(); } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLayout = findViewById(R.id.sample_main_layout); if (savedInstanceState == null) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); RuntimePermissionsFragment fragment = new RuntimePermissionsFragment(); transaction.replace(R.id.sample_content_fragment, fragment); transaction.commit(); } } } 

Modifiqué el código de @ Modge, en mi caso necesitaba pedir permiso de IMEI y la mejor manera de hacerlo es durante el tiempo de ejecución, así que hice esto:

  /** * A loading screen after AppIntroActivity. */ public class LoadingActivity extends BaseActivity { private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0; private TextView loading_tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loading); //trigger 'loadIMEI' loadIMEI(); /** Fading Transition Effect */ overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } /** * Called when the 'loadIMEI' function is triggered. */ 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) { // READ_PHONE_STATE permission has not been granted. requestReadPhoneStatePermission(); } else { // READ_PHONE_STATE permission is already been granted. doPermissionGrantedStuffs(); } } /** * Requests the READ_PHONE_STATE permission. * If the permission has been denied previously, a dialog will prompt the user to grant the * permission, otherwise it is requested directly. */ private void requestReadPhoneStatePermission() { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) { // Provide an additional rationale to the user if the permission was not granted // and the user would benefit from additional context for the use of the permission. // For example if the user has previously denied the permission. new AlertDialog.Builder(LoadingActivity.this) .setTitle("Permission Request") .setMessage(getString(R.string.permission_read_phone_state_rationale)) .setCancelable(false) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //re-request ActivityCompat.requestPermissions(LoadingActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE); } }) .setIcon(R.drawable.onlinlinew_warning_sign) .show(); } else { // READ_PHONE_STATE permission has not been granted yet. Request it directly. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, MY_PERMISSIONS_REQUEST_READ_PHONE_STATE); } } /** * Callback received when a permissions request has been completed. */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == MY_PERMISSIONS_REQUEST_READ_PHONE_STATE) { // Received permission result for READ_PHONE_STATE permission.est."); // Check if the only required permission has been granted if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // READ_PHONE_STATE permission has been granted, proceed with displaying IMEI Number //alertAlert(getString(R.string.permision_available_read_phone_state)); doPermissionGrantedStuffs(); } else { alertAlert(getString(R.string.permissions_not_granted_read_phone_state)); } } } private void alertAlert(String msg) { new AlertDialog.Builder(LoadingActivity.this) .setTitle("Permission Request") .setMessage(msg) .setCancelable(false) .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // do somthing here } }) .setIcon(R.drawable.onlinlinew_warning_sign) .show(); } public void doPermissionGrantedStuffs() { //Have an object of TelephonyManager TelephonyManager tm =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); //Get IMEI Number of Phone //////////////// for this example i only need the IMEI String IMEINumber=tm.getDeviceId(); /************************************************ * ********************************************** * This is just an icing on the cake * the following are other children of TELEPHONY_SERVICE * //Get Subscriber ID String subscriberID=tm.getDeviceId(); //Get SIM Serial Number String SIMSerialNumber=tm.getSimSerialNumber(); //Get Network Country ISO Code String networkCountryISO=tm.getNetworkCountryIso(); //Get SIM Country ISO Code String SIMCountryISO=tm.getSimCountryIso(); //Get the device software version String softwareVersion=tm.getDeviceSoftwareVersion() //Get the Voice mail number String voiceMailNumber=tm.getVoiceMailNumber(); //Get the Phone Type CDMA/GSM/NONE int phoneType=tm.getPhoneType(); switch (phoneType) { case (TelephonyManager.PHONE_TYPE_CDMA): // your code break; case (TelephonyManager.PHONE_TYPE_GSM) // your code break; case (TelephonyManager.PHONE_TYPE_NONE): // your code break; } //Find whether the Phone is in Roaming, returns true if in roaming boolean isRoaming=tm.isNetworkRoaming(); if(isRoaming) phoneDetails+="\nIs In Roaming : "+"YES"; else phoneDetails+="\nIs In Roaming : "+"NO"; //Get the SIM state int SIMState=tm.getSimState(); switch(SIMState) { case TelephonyManager.SIM_STATE_ABSENT : // your code break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED : // your code break; case TelephonyManager.SIM_STATE_PIN_REQUIRED : // your code break; case TelephonyManager.SIM_STATE_PUK_REQUIRED : // your code break; case TelephonyManager.SIM_STATE_READY : // your code break; case TelephonyManager.SIM_STATE_UNKNOWN : // your code break; } */ // Now read the desired content to a textview. loading_tv2 = (TextView) findViewById(R.id.loading_tv2); loading_tv2.setText(IMEINumber); } } 

Espero que esto te ayude a ti o a alguien.

Hice algo como esto:

  if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_READ_PHONE_STATE); } else { setDeviceImei(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (requestCode == PERMISSIONS_REQUEST_READ_PHONE_STATE && grantResults[0] == PackageManager.PERMISSION_GRANTED) { setDeviceImei(); } } private void setDeviceImei() { mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); //Code ......... .............. } 

¿Cómo obtendré la Lista de Permiso que necesito establecer en tiempo de ejecución …?

Podemos encontrar todos los permisos peligrosos de https://developer.android.com/guide/topics/permissions/requesting.html

Sin embargo, creo que es un poco molesto comprobar y solicitar permisos peligrosos en tiempo de ejecución. Así que me las arreglo con TARGET_SDK_VERSION = 22, un chico perezoso (╰_╯)