gpt4 book ai didi

Android NEXT_ALARM_FORMATTED 字符串到毫秒

转载 作者:行者123 更新时间:2023-11-29 15:23:48 26 4
gpt4 key购买 nike

现在我可以在字符串值中获取下一个警报。我想在毫秒内得到它。这是我尝试过的方法,但没有用。我也使用 Locale.US,但希望它适用于任何 Locale。请指教

String nextAlarm = android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.NEXT_ALARM_FORMATTED);

DateFormat format = new SimpleDateFormat("EEE hh:mm aa", Locale.US);
long nextAlarmTime = 0;
try {
Date date = format.parse(nextAlarm);
nextAlarmTime = date.getTime();
} catch (Exception e) {
}

long curTime = System.currentTimeMillis();
long diff = nextAlarmTime - curTime;

//diff would represent the time in milliseconds

最佳答案

在具有 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);

检索到的字符串的格式取决于 i.a.在运行设备的区域设置上。字符串由

  • 时间的数字(阿拉伯语、印度语、...?)

  • 字符串来自

    String[] weekdays = DateFormatSymbols.getInstance().getShortWeekdays();
  • 字符串来自

    String[] amPm = DateFormatSymbols.getInstance().getAmPmStrings();

您不能依赖工作日时间 AmPm 组件的顺序。例如,AmPm-String 并不总是放在末尾。在韩国,它通常放在工作日和时间之间。


我的解决方案:

AlarmTimeTool.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)));
}
}
}

关于Android NEXT_ALARM_FORMATTED 字符串到毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com