在具有 API 21 及更高版本的设备上,您可以通过以下方式实现毫秒数getSystemService()
:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo info =
((AlarmManager)getSystemService(Context.ALARM_SERVICE)).getNextAlarmClock();
if (info != null) {
long alarmTime = info.getTriggerTime();
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd hh:mm a", Locale.US);
Log.d(getClass().getSimpleName(),
simpleDateFormat.format(new Date(alarmTime)));
}
}
在旧设备上,您必须在数据库中查找警报字符串:
String nextAlarm = Settings.System.getString(getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
检索到的字符串的格式取决于运行设备的区域设置。字符串由
您不能依赖 weekday-time-AmPm-components 的顺序。例如,AmPm-String 并不总是放在最后。在韩国,它通常放在工作日和时间之间。
我的解决方案:
报警时间工具.java:
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AlarmTimeTool {
public static long getNextAlarm(String alarm, String[] weekdays, String[] amPm) {
int alarmHours = -1, alarmMinutes = -1;
int alarmWeekday = -1; // Calendar.SUNDAY = 1; Calendar.SATURDAY = 7;
int alarmAmPm = -1; // Calendar.AM = 0; Calendar.PM = 1;
for (String piece: alarm.split("\\s")) {
if (alarmWeekday == -1) {
alarmWeekday = getWeekday(piece, weekdays);
if (alarmWeekday != -1) {
continue;
}
}
if (alarmHours == -1) {
int[] hoursMinutes = getTime(piece);
alarmHours = hoursMinutes[0];
alarmMinutes = hoursMinutes[1];
if (alarmHours != -1) {
continue;
}
}
if (alarmAmPm == -1) {
alarmAmPm = getAmPm(piece, amPm);
}
}
if (alarmWeekday == -1 || alarmHours == -1) {
throw new RuntimeException("could not fetch alarm week or hour");
}
Calendar now = Calendar.getInstance();
Calendar nextAlarm = Calendar.getInstance();
nextAlarm.set(Calendar.DAY_OF_WEEK, alarmWeekday);
nextAlarm.set(Calendar.MINUTE, alarmMinutes);
nextAlarm.set(Calendar.SECOND, 0);
nextAlarm.set(Calendar.MILLISECOND, 0);
if (alarmAmPm == -1) {
nextAlarm.set(Calendar.HOUR_OF_DAY, alarmHours);
} else {
nextAlarm.set(Calendar.AM_PM, alarmAmPm);
nextAlarm.set(Calendar.HOUR, alarmHours % 12);
}
if (nextAlarm.before(now)) {
nextAlarm.add(Calendar.DAY_OF_MONTH, 7);
}
return nextAlarm.getTimeInMillis();
}
private static int[] getTime(String piece) {
int hours = -1;
int minutes = -1;
// Android only (\d has different meanings):
Pattern p = Pattern.compile("(\\d{1,2})\\D(\\d{2})");
Matcher m = p.matcher(piece);
if (m.find()) {
hours = Integer.parseInt(m.group(1));
minutes = Integer.parseInt(m.group(2));
}
int[] hoursMinutes = {hours, minutes};
return hoursMinutes;
}
private static int getWeekday(String piece, String[] weekdays) {
for (int i = 1; i < weekdays.length; i++) {
if (piece.contains(weekdays[i])) {
return i;
}
}
return -1;
}
private static int getAmPm(String piece, String[] amPm) {
for (int i = 0; i < amPm.length; i++) {
if (piece.contains(amPm[i])) {
return i;
}
}
return -1;
}
}
MainActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String nextAlarm = Settings.System.getString(getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
if (nextAlarm != null && !nextAlarm.isEmpty()) {
Log.d(getClass().getSimpleName(), nextAlarm);
String[] weekdays = DateFormatSymbols.getInstance().getShortWeekdays();
String[] amPm = DateFormatSymbols.getInstance().getAmPmStrings();
long alarmTime = AlarmTimeTool.getNextAlarm(nextAlarm, weekdays, amPm);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyy-MM-dd hh:mm a", Locale.US);
Log.d(getClass().getSimpleName(), simpleDateFormat.format(new Date(alarmTime)));
}
}
}