1

Tengo el siguiente codigo:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
    String networkOperator = telephonyManager.getNetworkOperator();
    String mcc = networkOperator.substring(0, 3);
Log.d("Tag=>", mcc);

Los permisos en el manifest estan habilitados, el logcat muestra lo siguiente:

01-31 13:17:15.787 4996-4996/? I/libpersona: KNOX_SDCARD checking this for 10317 01-31 13:17:15.787 4996-4996/? I/libpersona: KNOX_SDCARD not a persona 01-31 13:17:15.791 4996-4996/? E/Zygote: accessInfo : 0 01-31 13:17:15.800 4996-4996/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[1], Con:u:r:zygote:s0 SPD:SEPF_SECMOBILE_7.0_0006 RAM:SEPF_SECMOBILE_7.0_0004, [-1 -1 0 1 0 1] 01-31 13:17:15.801 4996-4996/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.israel.localizacion2 01-31 13:17:15.804 4996-4996/? I/art: Late-enabling -Xcheck:jni 01-31 13:17:15.867 4996-4996/? D/TimaKeyStoreProvider: TimaSignature is unavailable 01-31 13:17:15.867 4996-4996/? D/ActivityThread: Added TimaKeyStore provider 01-31 13:17:16.378 4996-4996/com.example.israel.localizacion2 W/System: ClassLoader referenced unknown path: /data/app/com.example.israel.localizacion2-1/lib/arm64 01-31 13:17:16.399 4996-4996/com.example.israel.localizacion2 D/ContextRelationMgrBrdg: loadKlass() : caller=com.samsung.android.bridge.multiscreen.common.ContextRelationManagerBridge.:28 android.app.LoadedApk.makeApplication:833 01-31 13:17:16.425 4996-4996/com.example.israel.localizacion2 I/FirebaseInitProvider: FirebaseApp initialization unsuccessful 01-31 13:17:16.429 4996-4996/com.example.israel.localizacion2 I/InstantRun: starting instant run server: is main process 01-31 13:17:16.555 4996-4996/com.example.israel.localizacion2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 01-31 13:17:16.695 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.696 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.702 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.703 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.704 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.706 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.707 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.708 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.709 4996-4996/com.example.israel.localizacion2 D/TextView: setTypeface with style : 0 01-31 13:17:16.746 4996-4996/com.example.israel.localizacion2 D/InputTransport: Input channel constructed: fd=77 01-31 13:17:16.748 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: setView = DecorView@5f843e3[MainActivity] touchMode=true 01-31 13:17:16.806 4996-5017/com.example.israel.localizacion2 I/OpenGLRenderer: Initialized EGL, version 1.4 01-31 13:17:16.806 4996-5017/com.example.israel.localizacion2 D/OpenGLRenderer: Swap behavior 1 01-31 13:17:16.818 4996-5017/com.example.israel.localizacion2 D/libGLESv1: STS_GLApi : DTS is not allowed for Package : com.example.israel.localizacion2 01-31 13:17:16.822 4996-5017/com.example.israel.localizacion2 D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1080x1920]-format:1 01-31 13:17:16.856 4996-4996/com.example.israel.localizacion2 W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 01-31 13:17:16.869 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1 01-31 13:17:16.869 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 01-31 13:17:16.870 4996-4996/com.example.israel.localizacion2 I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus 01-31 13:17:16.891 4996-5011/com.example.israel.localizacion2 D/InputTransport: Input channel constructed: fd=84 01-31 13:17:30.825 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: ViewPostImeInputStage processPointer 0 01-31 13:17:30.827 4996-4996/com.example.israel.localizacion2 W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar 01-31 13:17:30.832 4996-4996/com.example.israel.localizacion2 E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]] 01-31 13:17:30.907 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: ViewPostImeInputStage processPointer 1 01-31 13:17:31.718 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: ViewPostImeInputStage processPointer 0 01-31 13:17:32.281 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: ViewPostImeInputStage processPointer 1 01-31 13:18:32.237 4996-4996/com.example.israel.localizacion2 I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus 01-31 13:18:32.251 4996-4996/com.example.israel.localizacion2 D/InputTransport: Input channel constructed: fd=87 01-31 13:18:32.251 4996-4996/com.example.israel.localizacion2 D/InputTransport: Input channel destroyed: fd=84 01-31 13:18:33.870 4996-4996/com.example.israel.localizacion2 D/ViewRootImpl@6c6d012[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0 01-31 13:18:34.152 4996-4996/com.example.israel.localizacion2 D/InputTransport: Input channel destroyed: fd=87

Pero no se observa nada en el log.d, este mismo código funciona en lollipop, que podría ser?, gracias por su ayuda.

2 respuestas 2

1

Tienes que solicitar permisos al usuario. Puedes leer de eso en este link

A partir de Android 6.0 (nivel de API 23), los usuarios conceden permisos a las apps mientras se ejecutan, no cuando instalan la app.

Para que compruebes que es error de falta de permisos, puedes ir a Configuracion -> Aplicaciones -> Tu aplicacion -> Permisos y habilitar el permiso

0

Tienes 2 problemas

1) Estas detectando si tienes o no permisos pero no los estas requiriendo.

2) El permiso que se debe requerir para usar para usar la clase TelephonyManager es:

Manifest.permission.READ_PHONE_STATE

Por lo tanto realiza esta modificación, agregando la petición de los mismos y el permiso READ_PHONE_STATE :

    int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 12345;
    TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION , Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE},
                MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

        return;
    }

Recuerda agregar el permisos en tu AndroidManifest.xml :

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

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.