我正在制作一个简单的闹钟应用,要研究BroadcastReceivers
,问题是getHours() and getMinutes()
仅适用于API 23.
这是我的示例代码:AlarmActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
setWidgets();
Intent intent = new Intent(this.context, AlarmReceivers.class);
}
public void setWidgets(){
timePicker = (TimePicker) findViewById(R.id.timePicker);
//Calendar calendar = Calendar.getInstance();
buttonSetAlarm = (Button) findViewById(R.id.buttonSetAlarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int getHour = timePicker.getHour();
int getMinute = timePicker.getMinute();
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getHour());
calendar.set(java.util.Calendar.MINUTE, timePicker.getMinute());
if (getHour > 12){
int twelve = getHour - 12;
}
if (getMinute < 10){
}
setText("On");
}
});
我坚持使用getHour()
和getMinute()
。
答案 0 :(得分:8)
由于namespace usbDevice {
public class usbFunctions {
public:
static __declspec(dllexport) LPCSTR usbInit();
static __declspec(dllexport) LPCSTR getDescriptors();
};
}
和getHour()
适用于API 23及更高版本,如果您的应用程序针对较低的API,则需要添加对它们的支持。
您可以通过添加以下内容来完成此操作:
getMinute()
{@ 1}}和if(Build.VERSION.SDK_INT < 23){
int getHour = timePicker.getCurrentHour();
int getMinute = timePicker.getCurrentMinute();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
} else{
int getHour = timePicker.getHour();
int getMinute = timePicker.getMinute();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
calendar.set(Calendar.MINUTE, timePicker.getMinute());
}
都已弃用,但仍适用于较低的API。
答案 1 :(得分:1)
您可以使用Calendar类中的get()Methode。
calendar.get(Calendar.HOUR);
calendar.get(Calendar.MINUTE);