I'm trying to call a LocationListener class from a Service, but It seems that the listener doesn't start. It worked well when I called it from an Activity. When I tryed the same (only deleting "this" parameter) it doesn't work.
It seems to be something wrong with the context parameter but I don't know how to solve it.
Calling from Activity (working):
LocationTest2 locationListener = new LocationTest2(getApplicationContext());
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Calling from Service (Not working):
LocationTest2 locationListener = new LocationTest2(getApplicationContext());
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Class: LocationTest2:
public class LocationTest2 extends Thread implements LocationListener {
private String TAG = "LocationTest2";
private Context context;
private boolean isOnline = false;
private LocationManager locationManager;
public LocationTest2(Context context) {
LogSys.e(TAG, "Serviço criado!");
this.context = context;
}
public void startListening() {
isOnline = true;
LogSys.e(TAG, "Ligando serviço");
Toast.makeText(context, "Ligando serviço!", Toast.LENGTH_SHORT).show();
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
public void stopListening() {
if(locationManager != null)
{
Toast.makeText(context, "Desligando serviço!", Toast.LENGTH_SHORT).show();
locationManager.removeUpdates(this);
}
isOnline = false;
}
public boolean isOnline()
{
return isOnline;
}
//=================================
@Override
public void onLocationChanged(Location location) {
String locationStr = "LOCATION WORKED 33!!! " + location.getLatitude() + " | " + location.getLongitude();
LogSys.e(TAG, locationStr);
Toast.makeText(context, locationStr, Toast.LENGTH_SHORT).show();
setLocationToFirebase(location);
}
public void setLocationToFirebase(Location location)
{
String tbl_cliente = GlobalVars.SQL_TABELA_CLIENTE_DEVICE_NAME;
String tbl_dispositivo = GlobalVars.SQL_TABELA_DISPOSITIVO_NAME;
String tbl_coordinates = GlobalVars.SQL_TABELA_COORDINATES_NAME;
DatabaseReference databaseReference;
FirebaseDatabase firebaseDatabase;
firebaseDatabase = FirebaseDatabase.getInstance();
try {
firebaseDatabase.setPersistenceEnabled(true);
}catch(DatabaseException e){
LogSys.e(TAG, "DatabaseException: " + e.getMessage());
}
databaseReference = firebaseDatabase.getReference();
Cliente clienteSQLite = Cliente.getClienteSQLite(context);
Dispositivo dispositivoSQLite = Dispositivo.getDispositivoSQLite(context);
DatabaseReference dbRef = databaseReference.child(tbl_cliente)
.child(clienteSQLite.getId())
.child(tbl_dispositivo)
.child(dispositivoSQLite.getId())
.child(tbl_coordinates).push();
String key = dbRef.getKey();
Coordenadas coords = new Coordenadas();
coords.setId(key);
coords.setLatitude(location.getLatitude());
coords.setLongitude(location.getLongitude());
coords.setData(DataHoraAtual.getData());
coords.setHora(DataHoraAtual.getHora());
dbRef.setValue(coords);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
Does someone has any idea? Thanks.
I've found the solution!
If you're trying to execute into a Thread something that depends on the context/activity param, maybe, it could not work. For example, Toasts don't work into common Threads if you use getApplicationContext() function.
Solution: Use a handler or runOnUiThread() function. They will execute your command lines on the main thread. See my example:
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable()
{
@Override
public void run() {
//Here I can use getApplicationContext() because I'm on the UI Thread Context (I'm using a handler to do so).
Toast.makeText(getApplicationContext(), "Starting listener...", Toast.LENGTH_SHORT).show();
startListening();
}
});