API 15中的TimePicker getHours(),getMinutes

时间:2016-08-17 14:40:29

标签: android broadcastreceiver android-alarms android-calendar android-timepicker

我正在制作一个简单的闹钟应用,要研究BroadcastReceivers,问题是getHours() and getMinutes()仅适用于API 23.

  • 有没有其他方法可以实现这一目标?
  • 如果是这样的话?是否可能的解决方案适用于更高的API,如24?

这是我的示例代码: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()

2 个答案:

答案 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);