2015-06-09 81 views
0

我有一個小部件,我想從設置活動中設置一些值。 我已保存使用此代碼值共享PREF:從遠程視圖中的共享首選項獲取值

MainActivity.editor.putInt("selected_theme", 1); 
      MainActivity.editor.commit(); 

而且在遠程視窗類我已經在的onUpdate方法是這樣完成的:

MainActivity.prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE); 
    MainActivity.editor = MainActivity.prefs.edit(); 

    int saved_value = MainActivity.prefs.getInt("selected_theme", 0); 
    Log.d("ggg", "receiver: " + saved_value); 

但它總是給我看重,這是默認的。我需要從共享首選項中得到類似1,2,3 ....的整數值,這些已在Activity類中完成。 在此先感謝:)

+0

將您的偏好值存儲在'應用程序級別' –

+0

是我已經完成的。我已經在活動課中保存了值。 – kiturk3

+0

您實際上是想根據用戶在widget中的輸入來執行某些操作嗎? –

回答

1

您應始終使用Utility Classes執行任務,如數據持久性(共享首選項,數據庫,序列化等)。
我在這裏爲您提供一個基本的模板:

GenericUtility.class

package com.your.packagename; 

import android.content.Context; 
import android.content.SharedPreferences; 

public class GenericUtility { 

    public static int getIntFromSharedPrefsForKey(String key, Context context) 
    { 
     int selectedValue = 0; 

     SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE); 
     selectedValue = prefs.getInt(key, 0); 

     return selectedValue; 
    } 

    public static boolean setIntToSharedPrefsForKey(String key, int value, Context context) 
    { 
     boolean savedSuccessfully = false; 

     SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 

     try 
     { 
      editor.putInt(key, value); 
      editor.apply(); 
      savedSuccessfully = true; 
     } 
     catch (Exception e) 
     { 
      savedSuccessfully = false; 
     } 

     return savedSuccessfully; 
    } 

    public static String getStringFromSharedPrefsForKey(String key, Context context) 
    { 
     String selectedValue = ""; 

     SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE); 
     selectedValue = prefs.getString(key, ""); 

     return selectedValue; 
    } 

    public static boolean setStringToSharedPrefsForKey(String key, String value, Context context) 
    { 
     boolean savedSuccessfully = false; 

     SharedPreferences prefs = context.getSharedPreferences("com.your.packagename", Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = prefs.edit(); 

     try 
     { 
      editor.putString(key, value); 
      editor.apply(); 
      savedSuccessfully = true; 
     } 
     catch (Exception e) 
     { 
      savedSuccessfully = false; 
     } 

     return savedSuccessfully; 
    } 
} 

用法示例:

數據保存在共享偏好:

GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, getApplicationContext()); 

個OR

GenericUtility.setIntToSharedPrefsForKey("selected_theme", 1, MyActivity.this)); 

用於檢索數據從共享偏好:

int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", getApplicationContext()); 

OR

int selectedValue = GenericUtility.getIntFromSharedPrefsForKey("selected_theme", MyActivity.this); 

我希望這有助於。

+0

看起來不錯...會檢查出來並回復給你:) – kiturk3

+0

當然,慢慢來。 –

+1

Awsm man ...問題解決了:) – kiturk3