我在从我的活动向appWidgetProvide类发送数据(字符串)时遇到了一些麻烦.
我有一个名为updateWidget的方法.当窗口小部件首次放置在主屏幕中时,窗口小部件配置活动会调用此方法.当用户将数据输入到该活动时,当它从我的一个活动接收数据时,onReceive方法也会调用此方法.
这是我的问题:小部件放在主屏幕中,所有数据都在正确的位置.但是当我的活动发送数据onReceive(使用意图和额外)时,数据不会通过.我只是在extras.getString上得到一个空字符串.
我之前使用过Intent来在活动之间发送数据,当我将数据发送到widget提供类时,是否需要做一些不同的事情?或者我只是愚蠢而且缺少明显的东西?
我附上了(我认为是)相关的代码.如果您需要任何澄清或更多代码,请与我们联系.
感谢您花时间阅读本文以及您可以提供的任何帮助,
干杯罗刹克
widget配置类中的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的意图(这是我的一个活动类)
- 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,id,title1);
- }
- }
- else {
- super.onReceive(context,intent);
- }
- }
- static void updateWidget(Context context,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 – 这可能是不匹配的