2013-08-30 67 views
3

我的朋友已经完成了一个应用程序,现在他为他的应用程序创建了一个小部件,它可以用来以列表视图的方式显示任务列表。首先当他拖动小部件并安装在主屏幕中时,它将工作正常,并以列表视图的方式显示任务。Listivew小工具没有自动更新

他的问题是,当他在他的应用程序中进行更改,然后回到他的主屏幕时,没有反应。他是用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); 
      } 

    } 
} 

回答

6

我建议你试试下面的代码粘贴:

ComponentName component=new ComponentName(context,WidgetTaskSchedular.class); 
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.listWidget); 
appWidgetManager.updateAppWidget(component, remoteView); 

notifyAppWidgetViewDataChanged() --->通知在所有指定AppWidget实例指定集合视图的无效数据的当前。

所以,当你调用notifyAppWidgetViewDataChanged()那么RemoteViewsFactoryonDataSetChanged()方法将被调用它作为你的ListView一个适配器。您可以在onDataSetChanged()上进行与网络服务/网络相关的操作,从数据库中获取数据操作以及其他操作,获取响应并将其添加到您的数据集(可能是ArrayList或任何此类Collection)。

您可以参考Using App Widgets with Collections

0

您是否尝试过解决这里提到:

How to update a Widget dynamically (Not waiting 30 min for onUpdate to be called)?

请注意,以上只是触发更新,小部件本身需要获取所需的数据。

请注意,尽管广播接收器可以在小部件中注册,但是在更新之后,它们可用于垃圾收集,因此不是可靠的解决方案。

最后的选择是注册一个AlarmManager来触发执行小部件更新的服务。