1

現在、次のアラームを文字列値で取得できます。ミリ秒単位で取得したいと思います。これが私が試したものですが、うまくいきません。また、私は Locale.US を使用していますが、どのロケールでも機能するようにしたいと考えています。お知らせ下さい

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
4

3 に答える 3

2

Date のコードはミリ秒ではありません ;-) しかし、次に getTime() を呼び出すと、ミリ秒単位で時間が取得されます。

次のアラームの時間に基づいて、寝る時間を計算するために小さなアプリでこのコードを使用しています。これは、さまざまなアプローチを 2 時間試行して作成されたものであるため、最適化されている可能性があります。

    public static Date getNextAlarm(Context context) {
    // let's collect short names of days :-)        
    DateFormatSymbols symbols = new DateFormatSymbols();
    // and fill with those names map...
    Map<String, Integer> map = new HashMap<String, Integer>();
    String[] dayNames = symbols.getShortWeekdays();
    // filing :-)
    map.put(dayNames[Calendar.MONDAY],Calendar.TUESDAY);
    map.put(dayNames[Calendar.TUESDAY],Calendar.WEDNESDAY);
    map.put(dayNames[Calendar.WEDNESDAY],Calendar.THURSDAY);
    map.put(dayNames[Calendar.THURSDAY],Calendar.FRIDAY);
    map.put(dayNames[Calendar.FRIDAY],Calendar.SATURDAY);
    map.put(dayNames[Calendar.SATURDAY],Calendar.SUNDAY);
    map.put(dayNames[Calendar.SUNDAY],Calendar.MONDAY);
    // Yeah, knowing next alarm will help.....
    String nextAlarm = Settings.System.getString(context.getContentResolver(),Settings.System.NEXT_ALARM_FORMATTED);
    // In case if it isn't set.....
    if ((nextAlarm==null) || ("".equals(nextAlarm))) return null;
    // let's see a day....
    String nextAlarmDay = nextAlarm.split(" ")[0];
    // and its number....
    int alarmDay = map.get(nextAlarmDay);

    // the same for day of week (I'm not sure why I didn't use Calendar.get(Calendar.DAY_OF_WEEK) here...
    Date now = new Date();      
    String dayOfWeek = new SimpleDateFormat("EE", Locale.getDefault()).format(now);     
    int today = map.get(dayOfWeek);

    // OK, so let's calculate how many days we have to next alarm :-)
    int daysToAlarm = alarmDay-today;
    // yep, sometimes it will  be negtive number so add 7.
    if (daysToAlarm<0) daysToAlarm+=7;



    // Now we will build date, and parse it.....
    try {
        Calendar cal2 = Calendar.getInstance();
        String str = cal2.get(Calendar.YEAR)+"-"+(cal2.get(Calendar.MONTH)+1)+"-"+(cal2.get(Calendar.DAY_OF_MONTH));

        SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-d hh:mm");

        cal2.setTime(df.parse(str+nextAlarm.substring(nextAlarm.indexOf(" "))));
        cal2.add(Calendar.DAY_OF_YEAR, daysToAlarm);
        // and return it
        return cal2.getTime();
    } catch (Exception e) {

    }
    // in case if we cannot calculate...
    return null;
}
于 2013-06-06T22:20:42.693 に答える
0

この変数の設定を担当する alarm clock app の Alarms.java を見ると、12/24 時間モードに応じて、正しい形式は「E h:mm aa」または「E k:mm」のいずれかになります。SimpleDateFormat を使用して解析し、Calendar を使用して有効なフィールドを現在の日付にコピーします。

String format = android.text.format.DateFormat.is24HourFormat(context) ? "E k:mm" : "E h:mm aa";
Calendar nextAlarmCal = Calendar.getInstance();
Calendar nextAlarmIncomplete = Calendar.getInstance();
nextAlarmIncomplete.setTime(new SimpleDateFormat(format).parse(nextAlarm));

// replace valid fields of the current time with what we got in nextAlarm
int[] fieldsToCopy = {Calendar.HOUR_OF_DAY,Calendar.MINUTE,Calendar.DAY_OF_WEEK};
for (int field : fieldsToCopy) {
   nextAlarmCal.set(field, nextAlarmIncomplete.get(field));
}
nextAlarmCal.set(Calendar.SECOND, 0);

// if the alarm is next week we have wrong date now (in the past). Adding 7 days should fix this 
if (nextAlarmCal.before(Calendar.getInstance())) {
   nextAlarmCal.add(Calendar.DATE, 7);
}
于 2013-11-01T13:26:42.460 に答える