2017-07-19 150 views
0

如何從Firebase中檢索數據並將其存儲在列表中?
需要檢索此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. 

錯誤說,沒有創建地理圍欄,但我不理解爲什麼。

回答

1

這是由於onDataChange()方法的異步行爲而發生的,該方法在您分配listageofence = geofences;之前調用。所以爲了解決這個問題,你需要使用geofences裏面的onDataChange()方法。如果你想在外面使用它,只需使用該方法調用一個方法ArrayList作爲參數,或者從post看到我的答案。

希望它有幫助。

0

我認爲listageofence在firebase傳遞到創建listageofence之前使用。

嘗試從onCreate()方法中刪除recuperarLocais();並將其放入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(); 
} 
+0

沒有給@UmarZaii,按照我所做的更改,但錯誤仍然存​​在 –