在这里需要一些帮助......如何创建一个按钮,当我的设备中的时间是早上6点到8点59分时可以看到...当时间是上午9点时按钮将消失。请帮助,我只是newBie在android中学习新东西。谢谢!
答案 0 :(得分:1)
您可以使用java.util.Calendar
Calendar SIX_AM = Calendar.getInstance();
SIX_AM.set(Calendar.HOUR_OF_DAY,6);
SIX_AM.set(Calendar.MINUTE,0);
SIX_AM.set(Calendar.SECOND,0);
Calendar NINE_AM = Calendar.getInstance();
NINE_AM.set(Calendar.HOUR_OF_DAY,9);
NINE_AM.set(Calendar.MINUTE,0);
NINE_AM.set(Calendar.SECOND,0);
Calendar SIX_PM = Calendar.getInstance();
SIX_PM.set(Calendar.HOUR_OF_DAY,18);
SIX_PM.set(Calendar.MINUTE,0);
SIX_PM.set(Calendar.SECOND,0);
Calendar NINE_PM = Calendar.getInstance();
NINE_PM.set(Calendar.HOUR_OF_DAY,21);
NINE_PM.set(Calendar.MINUTE,0);
NINE_PM.set(Calendar.SECOND,0);
Calendar current = Calendar.getInstance();
if(current.compareTo(SIX_AM)>=0 && current.compareTo(NINE_AM)<0) {
btnAm.setVisibility(View.VISIBLE);
} else {
btnAm.setVisibility(View.GONE);
}
if(current.compareTo(SIX_PM)>=0 && current.compareTo(NINE_PM)<0) {
btnPm.setVisibility(View.VISIBLE);
} else {
btnPm.setVisibility(View.GONE);
}
希望有所帮助
答案 1 :(得分:0)
在您的情况下,AlarmManager将起作用。 设置两个警报
How to start Service using Alarm Manager in Android?
对于你我建议使用setExact而不是setRepeating。这是代码......
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else
am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
请记住,setExact不提供重复功能,因此每次您必须再次从服务中设置它...