简体   繁体   English

如何在应用程序中仅在特定时间段内启用某些功能?

[英]How to enable certain functions only for a specific period of time in apps?

I am working on a Booking App ,where there are different time slots in a day(for example 11:30,12:30) in which users can book their ride.But I don't want the booking option to be open for the whole day,it should just be open from a certain time until the next day.I've heard this can be done by cloud functions,if so ,can anyone tell me how? 我正在使用预订应用程序,该应用程序在一天中有不同的时间段(例如11:30,12:30),用户可以在其中预订他们的行程。但是我不希望预订选项打开一整天,它应该只从某个时间开放到第二天。我听说这可以通过云功能来完成,如果可以的话,有人可以告诉我吗?

This is my BookingActivity.java 这是我的BookingActivity.java

public class BookingActivity extends AppCompatActivity {

private DatabaseReference mUser;
private DatabaseReference mTime1;
private DatabaseReference mTime2;
private DatabaseReference mCount1;
private DatabaseReference mCount2;
private FirebaseAuth mAuth;
private static final String TAG = "BookingActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking);

mAuth = FirebaseAuth.getInstance();

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FirebaseUser fu = mAuth.getCurrentUser();
final User newUser = new User(fu);

mUser = FirebaseDatabase.getInstance().getReference().child("Users");
mTime1 = FirebaseDatabase.getInstance().getReference().child("3:30");
mTime2 = FirebaseDatabase.getInstance().getReference().child("5:30");
mCount1 = FirebaseDatabase.getInstance().getReference().child("Count@3:30");
mCount2 = FirebaseDatabase.getInstance().getReference().child("Count@5:30");

findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Update(mTime1,mCount1,newUser);

    }
});

findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Update(mTime2,mCount2,newUser);
    }
});

}


public void Book(DatabaseReference mDatabase,User user) {

Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();

HashMap<String,String>datamap = new HashMap<>();

if(user!=null) {
    datamap.put("Name", user.getUserName());
    datamap.put("Email", user.getUserEmail());
    datamap.put("timestamp",ts);
    datamap.put("Limit", user.setRating("1"));
}

 mDatabase.push().setValue(datamap);

}

public void Update(final DatabaseReference mDatabase, DatabaseReference mref,final User user) {

    mref.runTransaction(new Transaction.Handler() {
    @NonNull
    @Override
    public Transaction.Result doTransaction(@NonNull MutableData mutableData) {

        Integer CurrentValue = mutableData.getValue(Integer.class);
        if(CurrentValue==null) {
            return Transaction.success(mutableData);
        }
        else if(CurrentValue<5 && user.getRating().equals("0")){
            mutableData.setValue(CurrentValue + 1);
            Book(mDatabase,user);
            runOnUiThread(new Runnable() {
                public void run() {
                    final Toast toast = Toast.makeText(BookingActivity.this,"Booked Successfully",Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
         }
         else{
            runOnUiThread(new Runnable() {
                public void run() {
                    final Toast toast = Toast.makeText(BookingActivity.this,"Maximum Limit Reached",Toast.LENGTH_SHORT);
                    toast.show();
                }
            });
        }
        return Transaction.success(mutableData);

    }

    @Override
    public void onComplete(@Nullable DatabaseError databaseError, boolean b, @Nullable DataSnapshot dataSnapshot) {

        Log.d(TAG, "Updating likes count transaction is completed");

    }
});
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case R.id.menuLogout:

        FirebaseAuth.getInstance().signOut();
        finish();
        startActivity(new Intent(this, MainActivity.class));

        break;
}

return true;
}

My database structure looks like this 我的数据库结构如下所示 在此处输入图片说明

You check the time and enable/disable the booking button based on it when your activity starts. 您可以检查时间并在活动开始时基于该时间启用/禁用预订按钮。 Put a similar check inside the booking activity so they can't launch it directly. 在预订活动中放入类似的支票,这样他们就不能直接启动它。

Such feature doesn't exist on Google Cloud Functions. 这种功能在Google Cloud Functions中不存在。 You may be referring to scheduling[1], but that is used to trigger calls to functions. 您可能指的是schedule [1],但这用于触发对函数的调用。

What you can do is implement that business logic into the function itself, leaving it always available on the HTTP level, but only having it actually book something at certain hours (get the current time with "new Date();"). 您可以做的就是将该业务逻辑实现到函数本身中,使其始终可以在HTTP级别上使用,但只能在某些时间实际预订某些东西(使用“ new Date();”获取当前时间)。

Cheers, 干杯,

Miguel 米格尔

[1] https://medium.com/google-cloud/google-cloud-functions-scheduling-cron-5657c2ae5212 [1] https://medium.com/google-cloud/google-cloud-functions-scheduling-cron-5657c2ae5212

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

相关问题 如何仅在 Android 中的特定时间段启用按钮? - How to enable a button only on a specific time period in Android? 如何限制Android应用程序仅在特定时间段内运行? - How to restrict the Android application to run only a certain time period? 如何在一段时间内禁用JButton? - How to disable a JButton for certain period of time? 仅在一段时间内检测到信标时显示通知 - Displaying a notification only when a beacon is detected for a certain period of time 在Java中,如何仅选择或过滤在特定时间段内创建的文件 - In Java, how to only pick or filter files created between during a specific time period 如何一般地对时间范围建模,以允许在任何时间段或更具体的时间段内使用范围 - How to model a time range generically that will allow a range for any period of time, or a more specific period of time 无论日期如何,如何在特定时间段内执行任务 - How to execute a task between a certain time period regardless of the Date 如何在 Anylogic 中每隔一段时间(如 1 小时)停止延迟? - How to stop dely every certain period of time (like 1 hour) in Anylogic? 如何限制用户在一定时间内与程序交互? - How to restrict the user from interacting with the program for a certain period of time? 如何在Android中的特定时间段后执行任务? - How to execute a task after a certain time period in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM