package com.dam.profesor.webservice; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.AsyncTask; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button datos; EditText latitud; EditText longitud; TextView resultado; ObtenerWebService hiloconexion; Location location; LocationManager locationManager; LocationListener locationListener; AlertDialog alert = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datos = (Button) findViewById(R.id.datos); latitud = (EditText) findViewById(R.id.latitud); longitud = (EditText) findViewById(R.id.longitud); resultado = (TextView) findViewById(R.id.resultado); datos.setOnClickListener(this); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); /****Mejora****/ if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) { AlertNoGps(); } /********/ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } else { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } } else { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); } MostrarLocalizacion(location); locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { MostrarLocalizacion(location); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); /* Datos de los proveedores */ //List<String> listaProveedores = locationManager.getAllProviders(); //LocationProvider proveedor1 = locationManager.getProvider(listaProveedores.get(0)); //int Accuracy = proveedor1.getAccuracy(); //boolean TieneAltitud = proveedor1.supportsAltitude(); /*----*/ /* Establezco criterios para buscar un proveedor */ //Criteria criteria = new Criteria(); //criteria.setAccuracy(Criteria.ACCURACY_FINE); //String mejorProveedor = locationManager.getBestProvider(criteria,true); /*-----*/ } private void AlertNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("El sistema GPS esta desactivado, ¿Desea activarlo?") .setCancelable(false) .setPositiveButton("Si", new DialogInterface.OnClickListener() { public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) { dialog.cancel(); } }); alert = builder.create(); alert.show(); } @Override protected void onDestroy(){ super.onDestroy(); if(alert != null) { alert.dismiss (); } } @Override protected void onPause() { super.onPause(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } else { locationManager.removeUpdates(locationListener); } } else { locationManager.removeUpdates(locationListener); } } @Override protected void onResume() { super.onResume(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } else { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } else { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } } public void MostrarLocalizacion(Location loc){ if (loc!=null){ hiloconexion = new ObtenerWebService(); hiloconexion.execute(String.valueOf(loc.getLatitude()),String.valueOf(loc.getLongitude())); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.datos: hiloconexion = new ObtenerWebService(); hiloconexion.execute(latitud.getText().toString(),longitud.getText().toString()); // Parámetros que recibe doInBackground break; default: break; } } public class ObtenerWebService extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { String cadena = "http://maps.googleapis.com/maps/api/geocode/json?latlng="; //http://maps.googleapis.com/maps/api/geocode/json?latlng=38.404593,-0.529534&sensor=false cadena = cadena + params[0]; cadena = cadena + ","; cadena = cadena + params[1]; cadena = cadena + "&sensor=false"; String devuelve = ""; URL url = null; // Url de donde queremos obtener información try { url = new URL(cadena); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //Abrir la conexión connection.setRequestProperty("User-Agent", "Mozilla/5.0" + " (Linux; Android 1.5; es-ES) Ejemplo HTTP"); //connection.setHeader("content-type", "application/json"); int respuesta = connection.getResponseCode(); StringBuilder result = new StringBuilder(); if (respuesta == HttpURLConnection.HTTP_OK){ InputStream in = new BufferedInputStream(connection.getInputStream()); // preparo la cadena de entrada BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // la introduzco en un BufferedReader // El siguiente proceso lo hago porque el JSONOBject necesita un String y tengo // que tranformar el BufferedReader a String. Esto lo hago a traves de un // StringBuilder. String line; while ((line = reader.readLine()) != null) { result.append(line); // Paso toda la entrada al StringBuilder } //Creamos un objeto JSONObject para poder acceder a los atributos (campos) del objeto. JSONObject respuestaJSON = new JSONObject(result.toString()); //Creo un JSONObject a partir del StringBuilder pasado a cadena //Accedemos al vector de resultados JSONArray resultJSON = respuestaJSON.getJSONArray("results"); // results es el nombre del campo en el JSON //Vamos obteniendo todos los campos que nos interesen. //En este caso obtenemos la primera dirección de los resultados. String direccion="SIN DATOS PARA ESA LONGITUD Y LATITUD"; if (resultJSON.length()>0){ direccion = resultJSON.getJSONObject(0).getString("formatted_address"); // dentro del results pasamos a Objeto la seccion formated_address } devuelve = "Dirección: " + direccion; // variable de salida que mandaré al onPostExecute para que actualice la UI } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return devuelve; } @Override protected void onCancelled(String aVoid) { super.onCancelled(aVoid); } @Override protected void onPostExecute(String aVoid) { resultado.setText(aVoid); Log.i("GPS",aVoid); //super.onPostExecute(aVoid); } @Override protected void onPreExecute() { resultado.setText(""); super.onPreExecute(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } } }