请申请系统权限并加入写入等权限
getDate();
getTime();
private void getTime() {
cal = Calendar.getInstance();
hour = cal.get(Calendar.HOUR_OF_DAY);
minute = cal.get(Calendar.MINUTE);
}
//获取当前日期
private void getDate() {
cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
}
我们可以通过安卓原生日期控件DatePickerDialog或者DatePicker来设置日期,相对来说,DatePickerDialog会简单一点,下面我们使用DatePickerDialog来设置日期,代码如下:
getDate();
isUpdateDate = false;
DatePickerDialog datePicker = new DatePickerDialog(DateTimeActivity.this, DatePickerDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
if (isUpdateDate) {
SystemUtil.setSysDate(DateTimeActivity.this, year, monthOfYear, dayOfMonth);
}
}
}, year, month, day);
datePicker.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.positive), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
isUpdateDate = true;
}
});
datePicker.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
isUpdateDate = false;
datePicker.dismiss();
}
});
datePicker.getDatePicker().setCalendarViewShown(false); //隐藏日历
datePicker.setCancelable(false);
datePicker.setCanceledOnTouchOutside(false);
datePicker.show();
getTime();
isUpdateTime = false;
TimePickerDialog timePicker = new TimePickerDialog(this, TimePickerDialog.THEME_HOLO_LIGHT, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (isUpdateTime) {
SystemUtil.setSysTime(DateTimeActivity.this, hourOfDay, minute);
}
}
}, hour, minute, false);
timePicker.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.positive), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
isUpdateTime = true;
}
});
timePicker.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
isUpdateTime = false;
timePicker.dismiss();
}
});
window = timePicker.getWindow();
params = window.getAttributes();
params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN;
//设置dialog位置
//params.y = 300;
window.setAttributes(params);
timePicker.setCancelable(false);
timePicker.setCanceledOnTouchOutside(false);
timePicker.show();
/**
* 设置系统的日期
*/
public static void setSysDate(Context context, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
long when = c.getTimeInMillis();
if (when / 1000 < Integer.MAX_VALUE) {
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
/**
* 设置系统的时间
*/
public static void setSysTime(Context context, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
long when = c.getTimeInMillis();
if (when / 1000 < Integer.MAX_VALUE) {
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
优化:isUpdateTime和isUpdateDate的值表示按下了取消和确认的标识,这个为了解决安卓低版本的bug,无论取消还是点击屏幕外会执行onDateSet和onTimeSet,所以我们加入取消按钮,然后设置点击屏幕外不能取消dialog,也就是setCancelable(false)和setCanceledOnTouchOutside(false)