Problem when using a service on Android

1

I have an application that sends the location from time to time, it occurred to me that it could be through a given service that can not be closed and has to keep working.

  

Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service android:name=".Servicio_Localizacion" android:exported="false"></service>
  

Class of Service

public class Servicio_Localizacion extends Service {
    public static String URL_UBICACION = "http://...../URL.php";
    RequestQueue requestQueue;
    double Longitud=1;
    double Latitud=1;
    String Direccion = "hola";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

@Override
public void onCreate() {
    //Toast.makeText(this,"Servicio Creado...",Toast.LENGTH_SHORT).show();
    super.onCreate();
}

@Override
public void onDestroy() {
    //Toast.makeText(this,"Servicio destruido...",Toast.LENGTH_SHORT).show();
    super.onDestroy();
}

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Localizacion Local = new Localizacion();
    Local.setMainActivity(this);
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

    }
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);



    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
                if (Longitud != 0 && Latitud != 0 && Direccion != null) {
                    StringRequest request = new StringRequest(Request.Method.POST, URL_UBICACION, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }) {
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> parameters = new HashMap<String, String>();
                            parameters.put("longitud", String.valueOf(Longitud));
                            parameters.put("latitud", String.valueOf(Latitud));
//##########################################################################
//mando la ubicacion mediante volley a la base de datos cada que se obtenga.
//##########################################################################
                            parameters.put("direccion", Direccion);
                            parameters.put("dispositivo", "Manuel Morales");
                            return parameters;
                        }
                    };
                    requestQueue.add(request);
                }
        }
    };
    timer.scheduleAtFixedRate(timerTask, 0, 5000);
    return START_STICKY;
}


/**********************************************************************************************/
/**Localizacion de Dispostivo movil**/
/**********************************************************************************************/
public void setLocation(Location loc) {
    //Obtener la direccion de la calle a partir de la latitud y la longitud
    if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
        try {
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            List<Address> list = geocoder.getFromLocation(
                    loc.getLatitude(), loc.getLongitude(), 1);
            if (!list.isEmpty()) {
                Address DirCalle = list.get(0);
                Direccion = DirCalle.getAddressLine(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Localizacion implements LocationListener {
    Servicio_Localizacion mainActivity;

    public Servicio_Localizacion getMainActivity() {
        return mainActivity;
    }

    public void setMainActivity(Servicio_Localizacion mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    public void onLocationChanged(Location loc) {
        Longitud = loc.getLongitude();
        Latitud = loc.getLatitude();
        this.mainActivity.setLocation(loc);
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}

}

  

ERROR Online:

mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);
  

Error Detail

     

java.lang.RuntimeException: Unable to start service   developersalpha.hassmovil.Servicio_Localizacion@864db1f with   Intent {cmp = developersalpha.hassmovil / .LocationService}:   java.lang.SecurityException: "network" location provider requires   ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

    
asked by DoubleM 20.03.2017 в 19:01
source

2 answers

1

Your code claims that you do not have permission in the manifest to access the location. You must add the following line to your manifest file:

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

leaving something similar to this:

<?xml version="1.0" encoding="utf-8"?>

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

</manifest>
    
answered by 20.03.2017 в 20:34
1

The error is defined in the LogCat message:

  

java.lang.SecurityException: "network" location provider requires   ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission.

For the operation of the Location Provider, you require the following permissions within your AndroidManifest.xml :

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

and it is important to ensure that they are within the <manifest> tag, since otherwise they can not be configured.

    
answered by 26.07.2017 в 18:26