我的朋友已完成一个应用程序,现在他为他的应用程序创建了一个小部件,可用于以列表视图方式显示任务列表.首先,当他拖动窗口小部件并安装在主屏幕时,它将正常工作,并以列表视图方
他的问题是,当他对他的应用程序进行更改然后回到他的主屏幕时,没有反映出任何变化.他正在使用onReceive()方法来更新小部件
import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.Uri; import android.util.Log; import android.widget.RemoteViews; public class WidgetTaskSchedular extends AppWidgetProvider { private final String TAG = "CalendarViewSample:" + this.getClass().getName(); SharedPreferences sharedprefer; @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if(intent.getAction().equals("update_widget")) { int[] appid=new int[1]; sharedprefer=context.getSharedPreferences("TaskWidget", 0); appid[0]=sharedprefer.getInt("widget_key",0); Log.i(TAG,"Appwidgetid"+appid[0]); onUpdate(context, AppWidgetManager.getInstance(context),appid); } } public static String EXTRA_WORD= "com.capsone.testing.calendar.WORD"; @SuppressWarnings("deprecation") @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for (int i=0; i<appWidgetIds.length; i++) { sharedprefer=context.getSharedPreferences("TaskWidget", 0); SharedPreferences.Editor editor=sharedprefer.edit(); editor.putInt("widget_key", appWidgetIds[i]); editor.commit(); int a= sharedprefer.getInt("widget_key", 0); Log.i(TAG,"Sharedpreferencewidgetid:"+a); //ID=appWidgetIds[i]; Log.i(TAG,"LengthofWidget:"+appWidgetIds.length); // Log.i(TAG,"TestAppWidget:"+ID); Intent intentWidgetService=new Intent(context, WidgetService.class); intentWidgetService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]); intentWidgetService.setData(Uri.parse(intentWidgetService.toUri(Intent.URI_INTENT_SCHEME))); RemoteViews remoteView=new RemoteViews(context.getPackageName(), R.layout.widgetlayout); remoteView.setRemoteAdapter(appWidgetIds[i], R.id.listWidget, intentWidgetService); Intent clickIntent=new Intent(context, ActionBarActivity.class); PendingIntent clickPendingIntent=PendingIntent .getActivity(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteView.setPendingIntentTemplate(R.id.listWidget, clickPendingIntent); ComponentName component=new ComponentName(context,WidgetTaskSchedular.class); appWidgetManager.updateAppWidget(component, remoteView); } } }我建议你试试下面粘贴的代码:
ComponentName component=new ComponentName(context,WidgetTaskSchedular.class); appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget); appWidgetManager.updateAppWidget(component, remoteView);
notifyAppWidgetViewDataChanged()—>通知所有指定的AppWidget实例中的指定集合视图,使其当前数据无效.
因此,当您调用notifyAppWidgetViewDataChanged()时,将调用RemoteViewsFactory的onDataSetChanged()方法,该方法用作ListView的适配器.您可以执行Web服务/网络相关操作,数据获取操作以及onDataSetChanged()中的其他操作,获取响应并将其添加到您的数据集(可能是ArrayList或任何此类集合).
你可以参考Using App Widgets with Collections