Can I display a toast message based on user's current time?
I need to make an app, Where when I open the screen from 1:00 am to 11:00 am it will display the toast message "Good morning" whereas if I open the screen at 6:00 pm to 11:00 pm it will display the toast "good night"
public class ToastDisplay extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
Toast.makeText(context,"Good Morning",Toast.LENGTH_SHORT).show();
}
}
}
int toastDurationInMilliSeconds = 10000;
Toast.LENGTH_SHORT is int, so you have to declare new int
What you can do i use Calendar class to get the current time. Check the time and display the toast accordingly.
Calendar currentTime = Calender.getInstance();
int hour = currentTime.get(Calendar.HOUR_OF_DAY);
if(Use your condition here) {
Display toast
} else if(Use your other condition) {
Display toast
}
Just a note, your hour would be in 24 hour format so you need to check keeping that in mind.