我正在创建一个小部件,它将从数据库中为用户显示一些数据。其中一个数据取决于可以在我的设置活动中设置的参数。我用 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");
}
在正常活动中,我可以加载权重类型。我想 updateAppWidget 函数应该以某种方式替换这个函数。