Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 5.0(API 21)上的TimePickerReference_Android_Android Preferences - Fatal编程技术网

Android 5.0(API 21)上的TimePickerReference

Android 5.0(API 21)上的TimePickerReference,android,android-preferences,Android,Android Preferences,我正在为我的项目构建一个ConfigActivity,并希望实现一个扩展了DialogPreference的TimePickerReference。 在Android 6.0(API 23)下,所有这些都可以正常工作。但是如果我尝试用Android 5.0(API 21)更改时间,更改后的时间将无法节省。 在寻找答案后,我发现API21肯定有一个Bug,导致从未调用过OnTimeChangedListener。 我的应用程序必须使用所有API级别>=16 这是我在config.xml中的首选项屏

我正在为我的项目构建一个ConfigActivity,并希望实现一个扩展了DialogPreference的
TimePickerReference
。 在Android 6.0(API 23)下,所有这些都可以正常工作。但是如果我尝试用Android 5.0(API 21)更改时间,更改后的时间将无法节省。 在寻找答案后,我发现API21肯定有一个Bug,导致从未调用过
OnTimeChangedListener
。 我的应用程序必须使用所有API级别>=16

这是我在config.xml中的
首选项屏幕中的类别

<PreferenceCategory android:title="@string/notifications" >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="notifications_enabled"
        android:title="@string/get_notifications" />
    <com.test.test2.ui.preferences.TimePreference
        android:defaultValue="@string/default_notification_time"
        android:key="notifications_time"
        android:dialogTitle="NotificationDialogTitle"
        android:title="@string/choose_notification_time"
        />
</PreferenceCategory>
}

我知道VALIDATION_表达式是假的,但首先我想有机会在Android 5.0上节省通知时间

public class TimePreference extends DialogPreference implements TimePicker.OnTimeChangedListener {

private int hour = 0;
private int minute = 0;

/**
 * The validation expression for this preference
 */
private static final String VALIDATION_EXPRESSION = "[0-2]*[0-9]:[0-5]*[0-9]";

/**
 * The default value for this preference
 */
private String defaultValue = getContext().getString(R.string.default_notification_time);


/**
 * @param context
 * @param attrs
 */
public TimePreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

/**
 * @param context
 * @param attrs
 * @param defStyle
 */
public TimePreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

/**
 * Initialize this preference
 */
private void initialize() {
    setPersistent(true);
}

@Override
public void onTimeChanged(TimePicker view, int hour, int minute) {
    this.hour = hour;
    this.minute = minute;
    //persistString(hour + ":" + minute);
}


@Override
protected View onCreateDialogView() {

    TimePicker tp = new TimePicker(getContext());
    tp.setIs24HourView(DateFormat.is24HourFormat(tp.getContext()));
    tp.setOnTimeChangedListener(this);
    //tp.setBackgroundResource(R.color.dialog_header_background_color);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    }
    int h = getHour();
    int m = getMinute();
    if (h >= 0 && m >= 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            tp.setHour(h);
            tp.setMinute(m);
        } else {
            tp.setCurrentHour(h);
            tp.setCurrentMinute(m);
        }
    }

    return tp;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    if (positiveResult) {
        if (isPersistent()) {
            String result = hour + ":" + minute;
            persistString(result);
            //mValueText.setText(getNiceTimestring());
            callChangeListener(result);
            SharedPreferences sharedPrefs = Test2.applicationContext.getSharedPreferences(Test2.packageName, Context.MODE_PRIVATE);

            if (sharedPrefs.getBoolean(GET_NOTIFICATIONS, true)) {
                NotificationCenter.setNotificationTime(hour, minute, getContext());
            }
        }
    }
}

@Override
public void setDefaultValue(Object defaultValue) {
    // BUG this method is never called if you use the 'android:defaultValue' attribute in your XML preference file,
    // not sure why it isn't

    super.setDefaultValue(defaultValue);

    if (!(defaultValue instanceof String)) {
        return;
    }

    if (!((String) defaultValue).matches(VALIDATION_EXPRESSION)) {
        return;
    }

    this.defaultValue = (String) defaultValue;
}

/**
 * Get the hour value (in 24 hour time)
 *
 * @return The hour value, will be 0 to 23 (inclusive)
 */
private int getHour() {
    String time = getPersistedString(this.defaultValue);
    if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
        return -1;
    }

    return Integer.valueOf(time.split(":")[0]);
}

/**
 * Get the minute value
 *
 * @return the minute value, will be 0 to 59 (inclusive)
 */
private int getMinute() {
    String time = getPersistedString(this.defaultValue);
    if (time == null || !time.matches(VALIDATION_EXPRESSION)) {
        return -1;
    }

    return Integer.valueOf(time.split(":")[1]);
}


public String getDefaultValue() {
    return defaultValue;
}