android – 将数据发送到我的(主屏幕)小部件

前端之家收集整理的这篇文章主要介绍了android – 将数据发送到我的(主屏幕)小部件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在从我的活动向appWidgetProvide类发送数据(字符串)时遇到了一些麻烦.

我有一个名为updateWidget的方法.当窗口小部件首次放置在主屏幕中时,窗口小部件配置活动会调用方法.当用户将数据输入到该活动时,当它从我的一个活动接收数据时,onReceive方法也会调用方法.

这是我的问题:小部件放在主屏幕中,所有数据都在正确的位置.但是当我的活动发送数据onReceive(使用意图和额外)时,数据不会通过.我只是在extras.getString上得到一个空字符串.

我之前使用过Intent来在活动之间发送数据,当我将数据发送到widget提供类时,是否需要做一些不同的事情?或者我只是愚蠢而且缺少明显的东西?

我附上了(我认为是)相关的代码.如果您需要任何澄清或更多代码,请与我们联系.

感谢您花时间阅读本文以及您可以提供的任何帮助,

干杯罗刹克

widget配置类中的onListItemClick.

  1. @Override
  2. protected void onListItemClick(ListView l,View v,int position,long id) {
  3. super.onListItemClick(l,v,position,id);
  4.  
  5. Cursor note = mDbHelper.fetchNote(id);
  6. startManagingCursor(note);
  7. String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE));
  8. String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
  9. loadData(title);
  10.  
  11.  
  12. Intent resultValue = new Intent();
  13. resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
  14. setResult(RESULT_OK,resultValue);
  15. finish();
  16. }
  17.  
  18.  
  19.  
  20. void loadData(String title) {
  21.  
  22. AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
  23.  
  24. SingleNote.updateWidget(this,appWidgetManager,mAppWidgetId,title);
  25.  
  26. }

将数据发送到onReceive的意图(这是我的一个活动类)

  1. private void updateWidget() {
  2. Intent i = new Intent(this,SingleNote.class);
  3. i.setAction(SingleNote.UPDATE_ACTION);
  4. Toast.makeText(getApplicationContext(),mTitleText.getText()+"from the activity",Toast.LENGTH_SHORT).show();//This works just fine
  5. i.putExtra("title",mTitleText.getText());
  6. sendBroadcast(i);

}

我的小部件提供课程

  1. public class SingleNote extends AppWidgetProvider {
  2.  
  3. public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget";
  4. private static NotesDbAdapter mDbHelper;
  5.  
  6.  
  7.  
  8. public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds) {
  9. final int N = appWidgetIds.length;
  10.  
  11. // Perform this loop procedure for each App Widget that belongs to this provider
  12. for (int i=0; i<N; i++) {
  13. int appWidgetId = appWidgetIds[i];
  14.  
  15.  
  16.  
  17. // Tell the AppWidgetManager to perform an update on the current app widget
  18. RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.singlenote_widget);
  19. appWidgetManager.updateAppWidget(appWidgetId,views);
  20.  
  21.  
  22.  
  23.  
  24. }
  25. }
  26.  
  27. @Override
  28. public void onReceive(Context context,Intent intent) {
  29.  
  30. String action = intent.getAction();
  31. Bundle extras = intent.getExtras();
  32. String title1 = extras.getString("title");//this value does not come through
  33. Toast.makeText(context,title1,Toast.LENGTH_LONG).show();//this gives an empty space
  34.  
  35. if (action != null && action.equals(UPDATE_ACTION)) {
  36. final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  37. ComponentName name = new ComponentName(context,SingleNote.class);
  38. int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
  39. final int N = appWidgetId.length;
  40. if (N < 1)
  41. {
  42. return ;
  43. }
  44. else {
  45. int id = appWidgetId[N-1];
  46. updateWidget(context,id,title1);
  47. }
  48. }
  49.  
  50. else {
  51. super.onReceive(context,intent);
  52. }
  53. }
  54.  
  55.  
  56.  
  57. static void updateWidget(Context context,int appWidgetId,String title){
  58.  
  59. RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.singlenote_widget);
  60. views.setTextViewText(R.id.single_note_title,title);
  61. appWidgetManager.updateAppWidget(appWidgetId,views);
  62.  
  63.  
  64.  
  65. }

解决方法

我建议尝试替换i.putExtra(“title”,mTitleText.getText());在updateWidget()中使用i.putExtra(“title”,mTitleText.getText().toString());
  1. String title1 = extras.getString("title");

期望字符串,并且mTitleText.getText()返回Editable – 这可能是不匹配的

猜你在找的Android相关文章