In an activity I have a button that automatically returns the street, the city and the country in which I am. This happens as long as I have the location activated on the mobile. If I am deactivated in this activity, logically it does not detect the position. I have to reactivate the location, and wait a while for me to reposition myself.
Seeing also the Google Maps app, I noticed that if you try to access your location and it is disabled, first ask you to activate it, and then return it to you immediately, without having to wait.
Is there any way that when I press the button, it asks me to activate the position and give it to me at the moment?
I pass the activity to the full with which for now, if I have the ubi activated, it returns the data to me at once.
package com.example.oftecnica2.agendajose;
import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Environment;
import android.os.Vibrator;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class CrearReunion extends AppCompatActivity implements LocationListener {
TextView txtreunion;
ArrayList<Reunion> reuniones = new ArrayList();
ImageView imgaceptar;
ImageView imgcancelar;
EditText txtgps;
String direccion = "/";
boolean unavez = false;
private LocationManager locationManager;
private String provider;
int pulsacion = 0;
Vibrator vibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crear_reunion);
txtreunion = (TextView) findViewById(R.id.txtreunion);
txtgps = (EditText) findViewById(R.id.txtgps);
setTitle("NUEVA REUNION");
imgaceptar = (ImageView) findViewById(R.id.imgaceptar);
imgcancelar = (ImageView) findViewById(R.id.imgcancelar);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
imgcancelar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
imgcancelar.setImageResource(R.drawable.cancel2);
vibrator.vibrate(30);
break;
case MotionEvent.ACTION_UP:
imgcancelar.setImageResource(R.drawable.cancel);
cancelar();
break;
}
return true;
}
});
imgaceptar.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
imgaceptar.setImageResource(R.drawable.ok2);
vibrator.vibrate(30);
break;
case MotionEvent.ACTION_UP:
imgaceptar.setImageResource(R.drawable.ok);
aceptar();
break;
}
return true;
}
});
}
public void aceptar() {
SharedPreferences prefs = getSharedPreferences("MisPreferencias", Context.MODE_PRIVATE);
int numeroreunion = prefs.getInt("numeroreunion", 0);
numeroreunion = numeroreunion + 1;
//creo la reunion
int id = numeroreunion;
String nombre = txtreunion.getText().toString();
String cadena = "";
Calendar calendar = Calendar.getInstance();
String dia = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
String mes = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String ano = String.valueOf(String.valueOf(calendar.get(Calendar.YEAR)));
String hora = String.valueOf(calendar.get(Calendar.HOUR));
String minutos = String.valueOf(calendar.get(Calendar.MINUTE));
String segundos = String.valueOf(calendar.get(Calendar.SECOND));
String direccionFinal =txtgps.getText().toString();
if (mes.length() == 1) {
mes = "0" + mes;
}
if (dia.length() == 1) {
dia = "0" + dia;
}
if (hora.length() == 1) {
hora = "0" + hora;
}
if (minutos.length() == 1) {
minutos = "0" + minutos;
}
if (segundos.length() == 1) {
segundos = "0" + segundos;
}
Date ahora = new Date();
String fecha = dia + "/" + mes + "/" + ano;
String horaFinal = hora + ":" + minutos;
Reunion reunion = new Reunion(id, nombre, fecha, horaFinal,direccionFinal);
//cargo las otras reuniones si las hay
File tarjeta = Environment.getExternalStorageDirectory();
File archivo = new File(tarjeta.getAbsolutePath() + File.separator + "Reuniones", "reuniones.txt");
try {
ObjectInputStream entrada = new ObjectInputStream(new FileInputStream(archivo));
reuniones = (ArrayList) entrada.readObject();
entrada.close();
System.out.println("tras leer");
System.out.println(reuniones.size());
reuniones.add(reunion);
} catch (Exception e) {
}
//creo la estructura de carpetas
File carpetaPrincipal = new File(tarjeta.getAbsolutePath() + File.separator + "Reuniones" + File.separator + String.valueOf(id));
File carpetaFotos = new File(carpetaPrincipal.getAbsolutePath() + File.separator + "fotos");
File notas = new File(carpetaPrincipal.getAbsolutePath() + File.separator + "notas.txt");
File datos = new File(carpetaPrincipal.getAbsolutePath() + File.separator + "datos.txt");
try {
carpetaPrincipal.mkdir();
carpetaFotos.mkdir();
notas.createNewFile();
datos.createNewFile();
//creo el molde de cliente en el fichero
ObjectOutputStream entrada = new ObjectOutputStream(new FileOutputStream(datos));
Cliente cliente = new Cliente("", "", "", "");
entrada.writeObject(cliente);
entrada.close();
//creo el molde
ObjectOutputStream entrada2 = new ObjectOutputStream(new FileOutputStream(notas));
ArrayList<Nota> notas2 = new ArrayList();
entrada2.writeObject(notas2);
entrada2.close();
} catch (Exception e) {
}
//guardo y acabo
try {
File archivo2 = new File(tarjeta.getAbsolutePath() + File.separator + "Reuniones", "reuniones.txt");
ObjectOutputStream salida = new ObjectOutputStream(new FileOutputStream(archivo2));
salida.writeObject(reuniones);
salida.close();
} catch (Exception e) {
}
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("numeroreunion", id);
editor.commit();
setResult(RESULT_OK);
finish();
}
public void cancelar() {
setResult(RESULT_CANCELED);
finish();
}
public void busqueda(View view) {
vibrator.vibrate(30);
txtgps.setText("");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.removeUpdates(this);
txtgps.setText(direccion);
unavez=false;
direccion="/";
}
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
@Override
protected void onPause() {
super.onPause();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (Exception e) {
e.printStackTrace();
}
if (addresses != null && unavez == false) {
Address address = addresses.get(0);
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
direccion += address.getAddressLine(i);
direccion += "/";
}
unavez = true;
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}