从onResume中的共享SharedPreferences获取值到onCreate

时间:2014-06-25 13:16:25

标签: android sharedpreferences

我做了像这样的共享偏好功能,并把它放在onResume中,但是我希望从{偏好onCreate()中的警报管理器中获取该偏好值 这是我的创建功能:

     private int interval;

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
          preference();
           Calendar cal = Calendar.getInstance();
           Intent itnStart = new Intent(this, LocationHistoryService.class);
      PendingIntent pintent = PendingIntent.getService(this, 0, itnStart, 0);
      AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
      alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),interval , pintent);
                 }

偏好xml

              <?xml version="1.0" encoding="utf-8"?>
      <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
       <PreferenceCategory
        android:title="Battery Notification"
        android:key="Battery_Notification" > 

         <CheckBoxPreference
            android:key="battery_notif"
            android:summary="Enable or disable battery notification"
            android:title="Enable battery notification"

         />
         </PreferenceCategory>
         <PreferenceCategory
        android:title="Update"
        android:key="Update" > 

         <ListPreference 
        android:key="updates_interval"
        android:title="Updates interval"
        android:summary="Define how often updates will be performed"
        android:defaultValue="6000" 
        android:entries="@array/updateInterval"
        android:entryValues="@array/updateIntervalValues"

            />    
        </PreferenceCategory>
          </PreferenceScreen>

arrays.xml:

                    <?xml version="1.0" encoding="utf-8"?>
                 <resources>
                 <string-array name="updateInterval">
                 <item name="60000">Every 1 minute</item>
                <item name="300000">Every 5 minutes</item>
                <item name="180000">Every 30 minutes</item>
                <item name="360000">Every 1 hour</item>
                <item name="2160000">Every 6 hours</item>
                </string-array>
<string-array name="updateIntervalValues">
    <item name="60000">60000</item>
    <item name="300000">300000</item>
    <item name="180000">180000</item>
    <item name="360000">360000</item>
    <item name="2160000">2160000</item>
</string-array>

   </resources>

偏好功能:

      public String preference(){
           String tempinterval;
          int interval;
      SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
       String updateTime = sharedPrefs.getString("updates_interval", "-1");
      StringBuilder build= new StringBuilder();
      build.append(updateTime);
      tempinterval=build.toString();
      interval=Integer.parseInt(tempinterval);
}
       this is my onresume:
    protected void onResume()
{
   super.onResume();



}

当我向全局变量声明间隔时,它总是返回0 ..当我将我的报警管理器置于首选功能时,当我打开主页时,该报警管理器总是激活,所以如何优先传递间隔值在onresume中向我的闹钟管理器调用oncreate的功能?

0 个答案:

没有答案