1
I am having a problem related to a Thread. I need to use 2 classes that are outside of it and is giving me the following error:
E/Androidruntime: FATAL EXCEPTION: Thread-3 Process: com.example.Safeway, PID: 8681 java.lang.Illegalstateexception: Can’t create Handler Inside thread that has not called Looper.prepare() at com.google.android.gms.common.internal.Preconditions.checkstate(Unknown Source:29) at com.google.android.gms.Internal.location.zzbm.zzc(Unknown Source:11) at com.google.android.gms.Internal.location.zzbm.zza(Unknown Source:3) at com.google.android.gms.Location.FusedLocationProviderClient.requestLocationUpdates(Unknown Source:4) at.example.Safeway.Mapservice$Checker.run(Mapservice.java:91)
The classes are: buildLocationRequest and buildLocationCallBack, but I don’t know how to make them work inside the thread, I saw that it was with Handlers but I couldn’t find anything that would really help me. I wanted to take advantage of the post to ask if when I deal with Handles I will be able to make a Real-Time database using Firebase to update the longitude and latitude of a specific user? Because I tried several other methods to do it within a service and until now I did not succeed.
public class MapService extends Service{
FirebaseDatabase bancoDeDados;
DatabaseReference usuarioBancoDeDados;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
LocationCallback locationCallback;
//Cria uma lista de threads para fazer a manutenção
public List<Verificador> threads = new ArrayList<Verificador>();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//Criou, chama apenas 1 vez
@Override
public void onCreate() {
super.onCreate();
}
// Será repetido diversas vezes
public int onStartCommand(Intent intent, int flag, int startID) {
Verificador verificador = new Verificador(startID);
verificador.start();
threads.add(verificador);
return (START_STICKY); // usar START_REDELIVER_INTENT caso o intent seja necessário o reenvio
}
class Verificador extends Thread {
public int startID;
boolean ativo = true;
public Verificador(int startID) {
this.startID = startID;
}
@SuppressLint("MissingPermission")
public void run() {
while (ativo) {
//Longitude/Latitude
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MapService.this);
fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback,Looper.myLooper());
}
stopSelf();
}
}
private void buildLocationRequest(){
Log.i("Debug","LOCATION REQUEST");
locationRequest = new LocationRequest();
locationRequest.setPriority(locationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setSmallestDisplacement(10);
}
private void buildLocationCallBack(){
locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location:locationResult.getLocations()){
usuarioBancoDeDados = bancoDeDados.getReference("Usuários");
usuarioBancoDeDados.child("Usuários").child(usuarioAtual.getNomeUsuario()).child("latitudeUsuario").setValue(location.getLatitude());
usuarioBancoDeDados.child("Usuários").child(usuarioAtual.getNomeUsuario()).child("longitudeUsuario").setValue(location.getLongitude());
}
}
};
}
//mata a tread
@Override
public void onDestroy() {
super.onDestroy();
Log.i("Debug","CABO");
for (int i = 0, tam = threads.size(); i < tam; i++){
threads.get(i).ativo = false;
}
}
}