如何从Firebase检索数据并存储在列表中? 需要检索此firebase列表以创建地理围栏, 我无法确定错误。
这是我的班级(服务):
public class ServicoLocalCliente extends Service implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener
{
private static final String TAG = ServicoLocalCliente.class.getSimpleName();
private GoogleApiClient googleApiClient;
private List<Geofence> listageofence = new ArrayList<Geofence>();
private List<Geofence> listgeo = new ArrayList<Geofence>();
private PendingIntent geofencePendingIntent;
private float GEOFENCE_RADIUS = 150f;
private Geofence geofence;
private Geofence.Builder builder;
private Double lat ;
private Double lng;
private String uid ;
private String meuUid;
private String[] listgeoflat;
private String[] listgeoflng;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference ref = database.getReference();
private String geof;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onCreate()
{
super.onCreate();
criarGoogleApi();
ref = FirebaseDatabase.getInstance().getReference().child("localizacao");
recuperarLocais();
}
private Geofence construirGeofences(String ui,Double lat,Double lng)
{
Geofence geofence = new Geofence.Builder()
.setRequestId(ui)
.setCircularRegion(lat,lng, GEOFENCE_RADIUS)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_EXIT)
.build();
return geofence;
}
private List<Geofence> recuperarLocais() {
ref.child("localizacao").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Geofence> geofences = new ArrayList<Geofence>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Double lat = Double.valueOf(ds.child("latitude").getValue().toString());
Double lng = Double.valueOf(ds.child("longitude").getValue().toString());
String ui = ds.getKey();
geofences.add(construirGeofences(ui, lat, lng));
listageofence = geofences;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.d("test", String.valueOf(listageofence.size()));
return listageofence;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i(TAG, "iniciar comando");
if (!googleApiClient.isConnected())
googleApiClient.connect();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
Log.i(TAG, "conectado " + bundle);
Location l = null;
try
{
l = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
} catch (SecurityException e)
{
e.printStackTrace();
}
if (l != null)
{
Log.i(TAG, "lat " + l.getLatitude());
Log.i(TAG, "lng " + l.getLongitude());
}
iniciarLocalizacao();
addGeofences();
}
private void addGeofences()
{
try
{
LocationServices.GeofencingApi.addGeofences(
googleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
);
} catch (SecurityException e)
{
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i)
{
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onLocationChanged(Location location)
{
Log.i(TAG, "lat carregada " + location.getLatitude());
Log.i(TAG, "lng carregada " + location.getLongitude());
Toast.makeText(getApplicationContext(), "Servico de Localização Carregado", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.i(TAG, "google api desconectado");
googleApiClient.unregisterConnectionCallbacks(this);
LocationServices.GeofencingApi.removeGeofences(googleApiClient, getGeofencePendingIntent());
googleApiClient.disconnect();
}
private void criarGoogleApi()
{
Log.d(TAG, "criarGoogleApi()");
if (googleApiClient == null)
{
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
private void iniciarLocalizacao()
{
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(10000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try
{
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);
} catch (SecurityException e)
{
e.printStackTrace();
}
}
private GeofencingRequest getGeofencingRequest()
{
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofences(listageofence);
return builder.build();
}
private PendingIntent getGeofencePendingIntent()
{
// Reutilize o PendingIntent se já o tivermos.
if (geofencePendingIntent != null)
{
return geofencePendingIntent;
}
Intent intent = new Intent(this, ServicoTransicIntent.class);
// Usamos FLAG_UPDATE_CURRENT para que recebamos a mesma intenção pendente quando
// chamando addGeofences () e removerGeofences().
geofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return geofencePendingIntent;
}
}
这是错误:
07-19 13:38:32.396 9197-9197/com.example.fernandosilveira.testegeofence E/UncaughtException: java.lang.IllegalArgumentException: No geofence has been added to this request.
at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.android.gms.location.GeofencingRequest$Builder.build(Unknown Source)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.getGeofencingRequest(ServicoLocalCliente.java:231)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.addGeofences(ServicoLocalCliente.java:157)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.onConnected(ServicoLocalCliente.java:147)
at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
at com.google.android.gms.internal.zzaat.zzo(Unknown Source)
at com.google.android.gms.internal.zzaar.zzwi(Unknown Source)
at com.google.android.gms.internal.zzaar.onConnected(Unknown Source)
at com.google.android.gms.internal.zzaav.onConnected(Unknown Source)
at com.google.android.gms.internal.zzaag.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzj.zzxG(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzb(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
at com.google.android.gms.common.internal.zzf$zze.zzxH(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-19 13:38:32.434 9197-9306/com.example.fernandosilveira.testegeofence I/DynamiteModule: Considering local module com.google.android.gms.tagmanager:9 and remote module com.google.android.gms.tagmanager:11
07-19 13:38:32.434 9197-9306/com.example.fernandosilveira.testegeofence I/DynamiteModule: Selected remote version of com.google.android.gms.tagmanager, version >= 11
07-19 13:38:32.496 9197-9306/com.example.fernandosilveira.testegeofence W/GoogleTagManager: No container asset found in /assets/containers. Checking top level /assets directory for container assets.
07-19 13:38:32.579 9197-9306/com.example.fernandosilveira.testegeofence W/GoogleTagManager: Tag Manager's event handler WILL NOT be installed (no container loaded)
07-19 13:38:32.579 9197-9306/com.example.fernandosilveira.testegeofence I/GoogleTagManager: Tag Manager initilization took 129ms
07-19 13:38:32.711 9197-9197/com.example.fernandosilveira.testegeofence E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fernandosilveira.testegeofence, PID: 9197
java.lang.IllegalArgumentException: No geofence has been added to this request.
at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.android.gms.location.GeofencingRequest$Builder.build(Unknown Source)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.getGeofencingRequest(ServicoLocalCliente.java:231)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.addGeofences(ServicoLocalCliente.java:157)
at com.example.fernandosilveira.testegeofence.Geofence.ServicoLocalCliente.onConnected(ServicoLocalCliente.java:147)
at com.google.android.gms.common.internal.zzm.zzq(Unknown Source)
at com.google.android.gms.internal.zzaat.zzo(Unknown Source)
at com.google.android.gms.internal.zzaar.zzwi(Unknown Source)
at com.google.android.gms.internal.zzaar.onConnected(Unknown Source)
at com.google.android.gms.internal.zzaav.onConnected(Unknown Source)
at com.google.android.gms.internal.zzaag.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzl$1.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzj.zzxG(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzb(Unknown Source)
at com.google.android.gms.common.internal.zzf$zza.zzu(Unknown Source)
at com.google.android.gms.common.internal.zzf$zze.zzxH(Unknown Source)
at com.google.android.gms.common.internal.zzf$zzd.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-19 13:38:32.727 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Sending crashes
07-19 13:38:32.735 9197-9324/com.example.fernandosilveira.testegeofence W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
07-19 13:38:33.771 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Response code: 200
07-19 13:38:33.774 9197-9324/com.example.fernandosilveira.testegeofence I/FirebaseCrash: Report sent with crash report id: d39b46e288000000
07-19 13:38:42.354 9197-9290/com.example.fernandosilveira.testegeofence W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
错误说地理围栏没有创建,但我不明白为什么。
答案 0 :(得分:1)
由于onDataChange()
方法的异步行为而发生这种情况,该方法在您分配listageofence = geofences;
之前被调用。因此,为了解决这个问题,您需要在geofences
方法中使用onDataChange()
。如果您想在外面使用它,只需使用ArrayList
作为参数调用方法,或者查看我的答案post。
希望它有所帮助。
答案 1 :(得分:0)
我认为在传递firebase中的列表以创建listageofence
之前会使用listageofence
。
尝试从recuperarLocais();
方法中删除onCreate()
并将其放在onConnected()
方法中,就在使用listageofence之前。这是在iniciarLocalizacao();
和addGeofences();
之前的。
@Override
public void onCreate()
{
super.onCreate();
criarGoogleApi();
ref = FirebaseDatabase.getInstance().getReference().child("localizacao");
// recuperarLocais() has been removed
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
Log.i(TAG, "conectado " + bundle);
Location l = null;
try
{
l = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
} catch (SecurityException e)
{
e.printStackTrace();
}
if (l != null)
{
Log.i(TAG, "lat " + l.getLatitude());
Log.i(TAG, "lng " + l.getLongitude());
}
recuperarLocais(); //Added here
iniciarLocalizacao();
addGeofences();
}