2015-10-05 78 views
0

我正在编写一个Android应用程序,每天在特定时间提醒您。我正在使用“警报”来执行此操作,并在“首选项”活动中使用TimePicker。在Genymotion上,TimePicker上的时间输出为UTC(epoch)时间,但在我的Galaxy s3上时间输出为负值。为什么在不同的Android设备上有不同的时间输出?

这里是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

我遇到了同样类型的问题,(即)在两个不同的设备中获取不同的时间值。通过用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

+0

被低估的人,说出原因。在下调之前阅读下调的必要性和规则。 – Sackurise

相关问题