Android:从Firebase获取当前日期和时间 [英] Android : Get current date and time from firebase

查看:389
本文介绍了Android:从Firebase获取当前日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏,用户可以获得每日奖励.我想从firebase服务器获取当前日期和时间,因为我不想依赖可以操纵的设备时间.
我经历了ServerValue. Firebase中的TIMESTAMP,但不知道如何使用.它会返回一个带有时间戳的值的地图.我如何将该时间戳转换为日期.当用户单击每日奖励时,我想从Firebase中获取今天"的日期.

I am developing a simple game where users get daily bonus.I want to get current date and time from firebase server as i don't want to rely on device time which could be manipulated.
I went through ServerValue.TIMESTAMP in firebase but don't know how to use it.It returns a map with value as timestamp.How can i convert that timestamp into date.I want to get Today's date from firebase when ever user clicks on daily bonus.

推荐答案

自从Firebase引入可调用函数,您可以通过在Firebase Cloud Functions中创建一个Callable Function轻松在应用程序中使用它. 在您的index.js中,创建一个Function并使其返回当前时间戳

Since Firebase Introduced Callable Functions you can use it in your app easily by creating a Callable Function in Firebase Cloud Functions. in your index.js ,create a Function and make it return the current timestamp

exports.getTime = functions.https.onCall((data,context)=>{
return Date.now()
})

然后将其部署到Firebase Cloud Functions

then deploy it to Firebase Cloud Functions

然后在您的Android应用中添加Callable Functions依赖项

then in your Android App add the Callable Functions dependency

implementation 'com.google.firebase:firebase-functions:16.1.0'

然后以这种方式从您的应用中调用该函数,并确保您输入的函数名称与云功能中的名称" getTime "

then call the function from your app like this, and make sure you are typing the same name of the function 'getTime' as in your Cloud Function

    FirebaseFunctions.getInstance().getHttpsCallable("getTime")
            .call().addOnSuccessListener(new OnSuccessListener<HttpsCallableResult>() {
        @Override
        public void onSuccess(HttpsCallableResult httpsCallableResult) {
            long timestamp = (long) httpsCallableResult.getData();

        }
    });

如果要在多个类中调用此方法,还可以创建一个Simple接口

you can also make a Simple interface if you want to call this method in multiple classes

public interface OnGetServerTime {
    void onSuccess(long timestamp);

    void onFailed();
}

 public void getServerTime(final OnGetServerTime onComplete) {
    FirebaseFunctions.getInstance().getHttpsCallable("getTime")
            .call()
            .addOnCompleteListener(new OnCompleteListener<HttpsCallableResult>() {
                @Override
                public void onComplete(@NonNull Task<HttpsCallableResult> task) {
                    if (task.isSuccessful()) {
                        long timestamp = (long) task.getResult().getData();
                        if (onComplete != null) {
                            onComplete.onSuccess(timestamp);
                        }
                    } else {
                        onComplete.onFailed();
                    }
                }
            });

}

这篇关于Android:从Firebase获取当前日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆