Problemas con el permiso de ACCESS FINE LOCATION

publicado por: Anonymous

Estoy tratando de dar permisos en tiempo de uso. El permiso que quiero dar es el de localización.

hago lo siguiente defino en el Manifest

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

En mi clase MainActivity pongo esto (dentro del método Oncreate)

   locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED ) {

        } else {

            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1000);

        }
    }

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 10, this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);

y luego escribo fuera del metodo oncreate lo siguiete

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 1000) {
        if (permissions.length == 0 && permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {

        }
    }

Pero al ejecutar por primera vez me sale este error

com.Localizador.gpssms E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.Localizador.gpssms, PID: 7819
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Localizador.gpssms/com.Localizador.gpssms.MainActivity}: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                    Caused by: java.lang.SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.
                                                                       at android.os.Parcel.readException(Parcel.java:1620)
                                                                       at android.os.Parcel.readException(Parcel.java:1573)
                                                                       at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:712)
                                                                       at android.location.LocationManager.requestLocationUpdates(LocationManager.java:908)
                                                                       at android.location.LocationManager.requestLocationUpdates(LocationManager.java:469)
                                                                       at com.unprofesorya.gpssms.MainActivity.onCreate(MainActivity.java:45)
                                                                       at android.app.Activity.performCreate(Activity.java:6904)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                       at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:148) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:7325) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Si me pueden decir como quitar el error, copie el código tal cual esta en los ejemplos de android

solución

El problema es que en realidad no se asignaron los permisos,

podrías pedirlos manualmente en tu Activity dentro del método onCreate() de esta forma:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        Log.i("Permisos", "Se tienen los permisos!");
    } else {
        ActivityCompat.requestPermissions(
                this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION }, 1222);
    }
}
Respondido por: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *