Android 如果没有创建Geofire,如何检查Geofire以注销用户?
问题摘要:我是Android Studio和Firebase与Geofire的新手,请温柔一点。我的目标是,当用户在我的应用程序的地图屏幕上而没有Geofire位置时,将其注销。我不知道如何在Geofire中查询“g”。如何查询“g”子级以查看它是否已创建?如果没有创建“g”,则需要注销。所以目标是查询“g”,并创建一个方法,如果“g”为null,则将用户注销 我做过的事情:许多Stackoverflow帖子没有使用Android Studio。我确实发现了这一点,但它并没有解决我的问题。我也试过了 onLocationChanges是我添加在线供应商的地方,我还想检查是否创建了“g”。Android 如果没有创建Geofire,如何检查Geofire以注销用户?,android,firebase-realtime-database,geofire,Android,Firebase Realtime Database,Geofire,问题摘要:我是Android Studio和Firebase与Geofire的新手,请温柔一点。我的目标是,当用户在我的应用程序的地图屏幕上而没有Geofire位置时,将其注销。我不知道如何在Geofire中查询“g”。如何查询“g”子级以查看它是否已创建?如果没有创建“g”,则需要注销。所以目标是查询“g”,并创建一个方法,如果“g”为null,则将用户注销 我做过的事情:许多Stackoverflow帖子没有使用Android Studio。我确实发现了这一点,但它并没有解决我的问题。我也试
@Override
public void onLocationChanged(Location location) {
// Adds VendorOnline Child to Firebase when Vendor is On This Activity
addVendorOnline();
// Log user out if they are on the map screen without a "VendorOnline" Uid
logVendorOutIfBuggy();
}
这是创建在线供应商并检查是否创建了“g”的方法:
private void addVendorOnline(){
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
String vendorId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("VendorOnline");
GeoFire vendorGeoFire = new GeoFire(vendorIdReference);
vendorGeoFire.setLocation(vendorId, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));
}
}
// Log user out if for some reason they do not have a "g" child but are on the map screen.
private void logVendorOutIfBuggy() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("g");
if(ref == null){
Toast.makeText(VendorMapsActivity.this, "Error", Toast.LENGTH_LONG).show();
FirebaseAuth.getInstance().signOut();
}
我预计会发生什么和实际发生什么:如果“g”等于null,我预计用户会注销,但现在用户不会注销
以下是调用
getLocation
查看供应商是否注册了位置的示例
从LocationCallback.onLocationResult的javadocs中:
使用键的当前位置调用此方法。位置
如果GeoFire中没有存储密钥的位置,则将为null
当您将位置写入密钥时,Geofire会自动创建/更新g
属性。只要使用Geofire,就不必为该属性专门做任何事情。这听起来有点像是一个好主意。通过检测g
创建,您试图完成什么?如果未检测到“g”,我将尝试注销用户。我将调用getLocation
获取供应商密钥,LocationCallback
应在onLocationCallback
中返回空值(如果从未设置(或删除)位置)。另外,如果存在“l”值而没有“g”,则会不一致,因此在任何位置推断没有“g”是公平的。你能给出一个示例代码吗?谢谢朋友,太棒了!这看起来正是我要找的。我在其他帖子中找不到类似的解决方案。我只是想澄清一下,我把vendorKey保留为“someVendorKey”,对吗?不,它将是作为图像的供应商的uid密钥(否则我会复制它)。啊,好的,我明白了。uid密钥将更改为每个登录用户的随机密钥。我使用匿名登录。有没有办法解释一下,你是最棒的。你的帮助正是我所需要的。非常感谢。我让它工作了<代码>如果(FirebaseAuth.getInstance().getCurrentUser()!=null){最终字符串user=FirebaseAuth.getInstance().getCurrentUser().getUid();DatabaseReference VendoryReference=FirebaseAuth.getInstance().getReference(“VendorOnline”);GeoFire vendorGeoFire=new GeoFire(VendoryReference);…
结合LocationCallBack,如果没有创建VendorOnline,它现在就可以工作了!
final String vendorKey = "someVendorKey";
geoFire.getLocation(vendorKey, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
if (key.compareTo(vendorKey) == 0) {
if (location == null) {
// vendor has not logged a location yet (or it was removed)
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});