我正在尝试从Service调用LocationListener类,但是似乎监听器没有启动。 当我从Activity调用它时,它运行良好。 当我尝试相同的操作时(仅删除“ this”参数)无效。
context参数似乎有问题,但我不知道如何解决。
通过活动进行通话(工作):
LocationTest2 locationListener = new LocationTest2(getApplicationContext());
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
通过服务呼叫(不起作用):
LocationTest2 locationListener = new LocationTest2(getApplicationContext());
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
类: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) {
}
}
有人有什么主意吗?谢谢。
答案 0 :(得分:0)
我找到了解决方案!
如果您要执行到线程中,则取决于 上下文/活动参数,可能无法正常工作。例如, 如果使用getApplicationContext(),敬酒将无法进入通用线程 功能。
解决方案:使用处理程序或runOnUiThread()函数。他们将在主线程上执行您的命令行。看看我的例子:
from importlib import reload