Back to project page MovisensGattSensorExample.
The source code is released under:
GNU General Public License
If you think the Android project MovisensGattSensorExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.movisens.gattsensorexample.activities; //from www .j a v a 2 s . c o m import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import android.R.string; import android.content.Context; import android.content.res.TypedArray; import android.preference.DialogPreference; import android.text.format.DateFormat; import android.util.AttributeSet; import android.view.View; import android.widget.TimePicker; public class TimePreference extends DialogPreference { private Calendar calendar; private TimePicker picker = null; private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.US); public TimePreference(Context ctxt, AttributeSet attrs) { super(ctxt, attrs); setPositiveButtonText(string.ok); setNegativeButtonText(string.cancel); calendar = new GregorianCalendar(); } @Override protected View onCreateDialogView() { picker = new TimePicker(getContext()); return (picker); } @Override protected void onBindDialogView(View v) { super.onBindDialogView(v); picker.setIs24HourView(DateFormat.is24HourFormat(getContext())); picker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY)); picker.setCurrentMinute(calendar.get(Calendar.MINUTE)); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (positiveResult) { calendar.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour()); calendar.set(Calendar.MINUTE, picker.getCurrentMinute()); CharSequence summary = getSummary(); setSummary(summary); if (callChangeListener(summary)) { persistString(summary.toString()); notifyChanged(); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return (a.getString(index)); } @Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { String time = null; if (restoreValue) { if (defaultValue == null) { time = getPersistedString("00:00"); } else { time = getPersistedString(defaultValue.toString()); } } else { time = defaultValue.toString(); } try { calendar.setTime(timeFormat.parse(time)); } catch (ParseException e) { e.printStackTrace(); } } @Override public CharSequence getSummary() { if (calendar == null) { return null; } return DateFormat.getTimeFormat(getContext()).format( new Date(calendar.getTimeInMillis())); } }