我想开发一个小部件......但是当我从AppWidgetProvider将字符串传递给窗口小部件时我遇到了问题...我是通过共享首选项来完成此操作
当我在SharedPreferences中存储变量时出现问题,当我尝试获取其值时,它不返回null值,但是这样:“”
虽然它在WidgetPrefs.xml中存在该变量,但是返回了这个值,我知道prefs变量有错误,但我不确定
这是WidgetConfig的代码:
public class ActivityWidgetConfig extends Activity implements OnClickListener{
private int widgetId = 0;
private String titlestring;
private String textstring;
private EditText etTitle;
private EditText etText;
private ImageButton btnTitle;
private ImageButton btnText;
private ImageView previewIcon;
private Button iconButton;
private Button createButton;
private int icon_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widgetconfig);
Intent intentOrigen = getIntent();
Bundle params = intentOrigen.getExtras();
widgetId = params.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
setResult(RESULT_CANCELED);
etTitle = (EditText) findViewById(R.id.etTitle);
etText = (EditText) findViewById(R.id.etText);
btnTitle = (ImageButton) findViewById(R.id.btnTitle);
btnText = (ImageButton) findViewById(R.id.btnText);
createButton = (Button)findViewById(R.id.createButton);
iconButton = (Button)findViewById(R.id.iconButton);
btnTitle.setOnClickListener(this);
btnText.setOnClickListener(this);
createButton.setOnClickListener(this);
iconButton.setOnClickListener(this);
SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
titlestring = prefs.getString("widgettitle", "");
textstring = prefs.getString("widgettext", "");
icon_id = prefs.getInt("widgeticon_id", android.R.drawable.stat_sys_warning);
etTitle.setText(titlestring);
etText.setText(textstring);
previewIcon = (ImageView) findViewById(R.id.previewIcon);
previewIcon.setImageResource(icon_id);
}
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
switch (v.getId()){
case R.id.btnTitle:
editor.remove("widgettitle");
editor.commit();
etTitle.setText("");
break;
case R.id.btnText:
editor.remove("widgettext");
editor.commit();
etText.setText("");
break;
case R.id.createButton:
editor.putString("widgettitle", titlestring);
final String textstring=etText.getText().toString();
editor.putString("widgettext", textstring);
editor.putInt("widgeticon_id", icon_id);
editor.commit();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ActivityWidgetConfig.this);
WidgetOne.updateWidget(ActivityWidgetConfig.this, appWidgetManager, widgetId);
Intent resultado = new Intent();
resultado.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultado);
finish();
break;
case R.id.iconButton:
Intent i = new Intent(this, ActivityIcons.class);
startActivityForResult(i, 1);
break;
}
}
}
这是小部件的代码:
public class WidgetOne extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++)
{
//ID del widget actual
int widgetId = appWidgetIds[i];
//Actualizamos el widget actual
//updateWidget(context, appWidgetManager, widgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.iamaner.ACTUALIZAR_WIDGET")) {
// //Obtenemos el widget manager y la lista de IDs de nuestro widget
// ComponentName wgt =
// new ComponentName(context, MiWidget.class);
//
// AppWidgetManager widgetManager =
// AppWidgetManager.getInstance(context);
//
// int[] appWidgetIds = widgetManager.getAppWidgetIds(wgt);
//
// //Actualizamos todos los widgets
// for(int i=0; i<appWidgetIds.length; i++)
// actualizarWidget(context, widgetManager, appWidgetIds[i]);
//Obtenemos el ID del widget a actualizar
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
//Obtenemos el widget manager de nuestro contexto
AppWidgetManager widgetManager = AppWidgetManager.getInstance(context);
//Actualizamos el widget
//if (widgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
// actualizarWidget(context, widgetManager, widgetId);
//}
}
super.onReceive(context, intent);
}
public static void updateWidget(Context context, AppWidgetManager appWidgetManager, int widgetId)
{
SharedPreferences prefs = context.getSharedPreferences("WidgetPrefs", Context.MODE_PRIVATE);
RemoteViews controles = new RemoteViews(context.getPackageName(), R.layout.widget_1);
//Asociamos los 'eventos' al widget
Intent intent = new Intent("com.iamaner.ACTUALIZAR_WIDGET");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgettitle", ""));
controles.setTextViewText(R.id.txtMensaje, prefs.getString("widgetext", ""));
controles.setImageViewResource(R.id.ivIcon, icon_id);
appWidgetManager.updateAppWidget(widgetId, controles);
}
}
答案 0 :(得分:0)
没关系,问题是我为sharedpreferences字符串指定了不同的名字......
通过在Widget类中执行此操作来解决它:
prefs.getString("widgettext","");