我在将 Activity 中的数据(字符串)发送到appWidgetProvide类时遇到麻烦。
我有一个叫做updateWidget的方法。当窗口小部件首次放置在主屏幕中时,窗口小部件configure Activity 会调用此方法。当用户将数据输入我的一个 Activity 中时,onReceive方法也会调用此方法。
这是我的问题:将小部件放置在主屏幕中,所有数据都放在正确的位置。但是,当我的 Activity 向onReceive发送数据(使用 Intent 和附加功能)时,数据无法通过,我在extras.getString上得到了一个空字符串。
之前,我曾使用过 Intent 在 Activity 之间发送数据,将数据发送到小部件提供类时是否需要做一些不同的事情?还是我只是愚蠢而缺少明显的东西?
我已经附上(我认为是)相关的代码。让我知道您是否需要任何澄清或更多代码。
感谢您抽出宝贵的时间阅读本文档,并感谢您提供的任何帮助,
干杯罗刹
小部件configure类中的onListItemClick。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor note = mDbHelper.fetchNote(id);
startManagingCursor(note);
String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE));
String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
loadData(title);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK,resultValue);
finish();
}
void loadData(String title) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title);
}
将数据发送到onReceive的 Intent (在我的 Activity 类之一中)
private void updateWidget() {
Intent i = new Intent(this, SingleNote.class);
i.setAction(SingleNote.UPDATE_ACTION);
Toast.makeText(getApplicationContext(),mTitleText.getText()+"from the activity",
Toast.LENGTH_SHORT).show();//This works just fine
i.putExtra("title", mTitleText.getText());
sendBroadcast(i);
}
我的小部件提供类
public class SingleNote extends AppWidgetProvider {
public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget";
private static NotesDbAdapter mDbHelper;
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Tell the AppWidgetManager to perform an update on the current app widget
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
String title1 = extras.getString("title");//this value does not come through
Toast.makeText(context, title1,Toast.LENGTH_LONG).show();//this gives an empty space
if (action != null && action.equals(UPDATE_ACTION)) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName name = new ComponentName(context, SingleNote.class);
int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
final int N = appWidgetId.length;
if (N < 1)
{
return ;
}
else {
int id = appWidgetId[N-1];
updateWidget(context, appWidgetManager, id ,title1);
}
}
else {
super.onReceive(context, intent);
}
}
static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String title){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
views.setTextViewText(R.id.single_note_title, title);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
最佳答案
我建议尝试将i.putExtra("title", mTitleText.getText());
中的updateWidget()
替换为i.putExtra("title", mTitleText.getText().toString());
String title1 = extras.getString("title");
需要字符串,并且
mTitleText.getText()
返回Editable
-这很可能不匹配