如何从时间戳对话框到偏好活动获得时间?

时间:2015-11-28 08:28:08

标签: android timepicker preferenceactivity

我有一个时间选择器对话框,我在偏好活动中使用它。我希望时间选择器选择的时间使用警报服务来安排任务。但我希望时间选择器选择的时间用于警报。我使用共享偏好但我不能使它工作。有人可以帮我吗?

这是TimePicker对话框

public class TimePreference  extends DialogPreference {
private int lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;
Context c;

public static int getHour(String time) {
    String[] pieces=time.split(":");

    return(Integer.parseInt(pieces[0]));
}

public static int getMinute(String time) {
    String[] pieces=time.split(":");

    return(Integer.parseInt(pieces[1]));
}

public TimePreference(Context ctxt, AttributeSet attrs) {
    super(ctxt, attrs);

    setPositiveButtonText("Set");
    setNegativeButtonText("Cancel");
}

@Override
protected View onCreateDialogView() {
    picker=new TimePicker(getContext());

    return(picker);
}

@Override
protected void onBindDialogView(View v) {
    super.onBindDialogView(v);

    picker.setCurrentHour(lastHour);
    picker.setCurrentMinute(lastMinute);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
        lastHour=picker.getCurrentHour();
        lastMinute=picker.getCurrentMinute();
        String hour=String.valueOf(lastHour);
        String minute=String.valueOf(lastMinute);

        String time=hour+":"+minute;

        //Toast.makeText(getContext(), time, Toast.LENGTH_SHORT).show();




        if (callChangeListener(time)) {
            persistString(time);
        }
    }
}

@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("06:00");
        }
        else {
            time=getPersistedString(defaultValue.toString());
        }
    }
    else {
        time=defaultValue.toString();
    }

    lastHour=getHour(time);
    lastMinute=getMinute(time);
}

}

这是首选项活动中的复选框首选项

  final Preference pref2 = getPreferenceManager().findPreference("son");
     pref2.setOnPreferenceChangeListener(new      Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (newValue.equals(true)) {
                // Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
                //set alarm will come here
                Long time = new GregorianCalendar().getTimeInMillis() + 5000;
                Intent i2 = new Intent(getActivity(), AlarmReceiver.class);
                AlarmManager alrm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                alrm.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(getActivity(), 1, i2, PendingIntent.FLAG_UPDATE_CURRENT));
                Toast.makeText(getActivity(), "Alarm Scheduled for 5 seconds from now", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getActivity(), "Schedule canceled", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });

这里我希望使用timepicker对话框选择的时间而不是我上面使用的“时间”。

这是首选项活动的xml文件

  <?xml version="1.0" encoding="utf-8"?>
  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


 <PreferenceCategory android:title="Schedule On">
  <CheckBoxPreference
    android:title="Schedule Power On"

    android:key="son"
    android:defaultValue="false"

    > </CheckBoxPreference>
<com.example.myapp.TimePreference
    android:key="timePrefA_Key"
    android:title="Pick Time"/>
</PreferenceCategory>
</PreferenceScreen>     

被修改

      @Override
public void onPause() {
    super.onPause();

    SharedPreferences.Editor editor = mSharedPref.edit(); // get the pref editor
    editor.putString("key1", stringtt); // assign value to the key
    editor.commit();  // save the editors modifications
}
@Override
public void onResume(){
    super.onResume();
  SharedPreferences.Editor editor2 = mSharedPref.edit();
    editor2.getString("key1", null);
}          

1 个答案:

答案 0 :(得分:3)

尝试以下代码我使用代码并检查,你将得到的newValue是你从时间偏好对话框中选择的时间。

 Preference preference = getPreferenceManager().findPreference("timePrefA_Key");
     preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                Log.d("Preference ", "" + preference);
                Log.d("newValue", "" + newValue);
                return true;
            }
        });