我正在编写一款Android应用,每天都会在特定时间提醒您。我使用Alarm来执行此操作,在Preferences活动中使用TimePicker。在Genymotion上,TimePicker上的时间输出为UTC(纪元)时间,但是在我的Galaxy s3上,时间输出为负数。
这是TimePicker类:
package com.maxmarksstudios.dialywomensselfdefense;
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;
import com.maxmarksstudios.dialywomensselfdefense.R;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimePreference extends DialogPreference {
private Calendar calendar;
private TimePicker picker = null;
public TimePreference(Context ctxt) {
this(ctxt, null);
}
public TimePreference(Context ctxt, AttributeSet attrs) {
this(ctxt, attrs, android.R.attr.dialogPreferenceStyle);
}
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText(R.string.set);
setNegativeButtonText(R.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.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());
setSummary(getSummary());
if (callChangeListener(calendar.getTimeInMillis())) {
persistLong(calendar.getTimeInMillis());
notifyChanged();
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return (a.getString(index));
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
if (restoreValue) {
if (defaultValue == null) {
calendar.setTimeInMillis(getPersistedLong(System.currentTimeMillis()));
} else {
calendar.setTimeInMillis(Long.parseLong(getPersistedString((String) defaultValue)) * 1000);
}
} else {
if (defaultValue == null) {
calendar.setTimeInMillis(System.currentTimeMillis());
} else {
calendar.setTimeInMillis(Long.parseLong((String) defaultValue) * 1000);
}
}
setSummary(getSummary());
}
@Override
public CharSequence getSummary() {
if (calendar == null) {
return null;
}
HomePage.alarmMethod(getContext());
return DateFormat.getTimeFormat(getContext()).format(new Date(calendar.getTimeInMillis()));
}
}
这是我调用的首选项,它似乎在每台设备上都有所改变。
public static void alarmMethod(Context context) {
//ALARM SECTION
AlarmManager alarms ;
Calendar alarmCalendar;
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
long longTime = (getPrefs.getLong("notifyTime", 18));
long x = (getPrefs.getLong("notifyTime", 18));
boolean alarmUse = getPrefs.getBoolean("notifyBox", true);
System.out.println("The time is " + longTime);
System.out.println("Hello " + System.currentTimeMillis());
x /= 1000;
x /= 60;
int alarmTimeMinute = (int)x%60;
x /= 60;
int alarmTimeHour = (int)x%24-4;
System.out.println(alarmTimeHour);
System.out.println("Hours (24hr) UTC - " + (x%24-4));
System.out.println("Hours (12hr) UTC - " + (x%12-4));
alarms = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
alarmCalendar = Calendar.getInstance();
alarmCalendar.setTimeInMillis(System.currentTimeMillis());
alarmCalendar.set(Calendar.HOUR_OF_DAY, alarmTimeHour);
alarmCalendar.set(Calendar.MINUTE, alarmTimeMinute);
Intent activate = new Intent(context, AlertDaily.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
long timeToAlarm = alarmCalendar.getTimeInMillis();
System.out.println("Militime is " + System.currentTimeMillis());
System.out.println(timeToAlarm);
if (alarmCalendar.getTimeInMillis() < System.currentTimeMillis())
{
timeToAlarm += (24*60*60*1000);
}
if(alarmUse) {
alarms.setRepeating(AlarmManager.RTC, timeToAlarm, 24 * 60 * 60 * 1000, alarmIntent);
} else {
alarms.cancel(alarmIntent);
System.out.println("You have disabled the alarm.");
}
}
为什么TimePicker在模拟器上给我不同的值而不是手机上的任何帮助都会很棒。
答案 0 :(得分:0)
我遇到了同样类型的问题,(即)在2个不同的设备中获得不同的时间值。我通过使用TimeZone和Locale声明GregorianCalendar来解决问题。
GregorianCalendar calendar = GregorianCalendar.getInstance(TimeZone.getDefault(),Locale.US);
在这种情况下尝试这样
public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
super(ctxt, attrs, defStyle);
setPositiveButtonText(R.string.set);
setNegativeButtonText(R.string.cancel);
// calendar = new GregorianCalendar(); Modify this line as below
calendar = new GregorianCalendar(TimeZone.getDefault(), Locale.US);
}
参考网址:Calendar.WEEK_OF_MONTH gives different results on two different devices