Sharedpreferences不适用于appWidgetProvider(getApplicationContext未定义)

时间:2012-05-07 22:02:36

标签: android widget sharedpreferences

我正在创建一个小部件,它将从数据库中为用户显示一些数据。其中一个数据取决于可以在我的设置活动中设置的参数。我用sharedpreferences保存这个参数,所以我可以在我的代码中的任何地方使用它。 在一个活动中我可以使用getApplicationContext,但在这里我告诉小部件该做什么,它不起作用。我应该使用什么而不是getApplicationContext?

修订版

public class plWidget extends AppWidgetProvider{

    SharedPreferences sharedPreferences;
     String loadedWeightType;

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "deleted", 2500).show();
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);

         String Wcal="0",Wfat="0",Wprot="0",Wcarb="0",Wsport="0";

         final int N = appWidgetIds.length;

         for (int i = 0;i<N;i++)
        {
            int awID = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);

            GlobalVars.setSulyType(loadedWeightType);           
        Log.i("SULYYYY", GlobalVars.getSulyType());

        long now = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date resultdate = new Date(now);
        Log.i("ASAS", sdf.format(resultdate));


        hornot database = new hornot(context);
        database.open();



         int ccc = database.checkDataExists(sdf.format(resultdate), sdf.format(resultdate));
        if (ccc==0){
            Log.i("nulla", "0");
            Log.i("nulla", GlobalVars.getSulyType());

            Wcal="0";
            Wfat="0";
            Wprot="0";
            Wcarb="0";
        }

        else{

        database.getDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wcal = GlobalVars.getSums();

        database.FATgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wfat = GlobalVars.getSums();

        database.PROTEINgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wprot = GlobalVars.getSums();

        database.CARBSgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
         Wcarb = GlobalVars.getSums();

                }

        int ddd = database.checkDataExistsSports(sdf.format(resultdate), sdf.format(resultdate));
        if (ddd==0){
            Wsport="0";

        }
        else{

            if (loadedWeightType.equals("kilogramm"))
                    {
            database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
            // Wsport = GlobalVars.getSums();
             Wsport= "kilogramm";


                    }
            else if (loadedWeightType.equals("pound"))
                    {
            database.SportgetDateFromAndToFromDatePicker(sdf.format(resultdate), sdf.format(resultdate));
            Wsport="pound";
                    }

        }

        RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
        v.setTextViewText(R.id.tvwidgetUpdate, Wcal+Wfat+Wprot+Wcarb+Wsport);
        appWidgetManager.updateAppWidget(awID, v);
        database.close();



        }


    }

    public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { 
        SharedPreferences prefs = context.getSharedPreferences(plWidget.class + Integer.toString(appWidgetId),
                Context.MODE_WORLD_READABLE);
         loadedWeightType= prefs.getString("weighttype", "kilogramm");
    }

}

提前致谢!

更新

像往常一样,我执行加载功能:

 public void LoadWeightType(){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        loadedWeightType= sharedPreferences.getString("weighttype", "kilogramm");
      }

在正常活动中,我可以加载weighttype。我想updateAppWidget函数应该以某种方式替换这个函数。

1 个答案:

答案 0 :(得分:1)

public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(MyConfigActivity.NAME + Integer.toString(appWidgetId), Context.MODE_WORLD_READABLE);

我从onUpdate为我的小部件的每个实例调用updateAppWidget,传入参数。传入的Context中定义了getSharedPreferences,并根据配置活动的名称和窗口小部件的id指定要获取的首选项集。这是getSharedPreferences函数的sdk参考:http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29