简体   繁体   English

如何将设备连接到Android Firebase数据库

[英]How can I connect a device to my Android firebase database

I am fairly new to Android development. 我是Android开发的新手。 I am working on an app which I have connected to a firebase database and everything works fine. 我正在开发一个已连接到Firebase数据库的应用程序,并且一切正常。 Now I am trying to connect a device which will send a like an attendance details of each users when they clock in on that device to the firebase database. 现在,我正在尝试连接一个设备,当每个用户在该设备上登录Firebase数据库时,该设备将向每个用户发送出勤信息。 Do I need a http interface to do this or an API 我需要http接口来执行此操作还是需要API

Try this 尝试这个

Declare few variables: 声明几个变量:

private FirebaseUser user;
private FirebaseDatabase database;
private DatabaseReference mRef;

Initialise them inside onCreate or somewhere: 在onCreate或其他地方初始化它们:

    user = FirebaseAuth.getInstance().getCurrentUser();
    database = FirebaseDatabase.getInstance();
    mRef = database.getReference("users").child(user.getUid()).child("attendance"); //different path for each user

Add value event listener 增值事件监听器

mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Integer attendance = dataSnapshot.getValue(Integer.class);
                attendance++;
                myRef.setValue(attendance);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

No you don't need any http interface to read and write value to firebase database . 不,您不需要任何http接口即可将值读写到Firebase数据库中。 You can refer to official documentation here 您可以在这里参考官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM