2012-05-28 54 views
0

我正在试图制作一个简单的小部件,它可以缓慢地旋转/遍历用户遇到麻烦的一组提示卡(类似于windows phone live tile)。为什么我的小部件没有更新?

我能够创建窗口小部件,它显示了默认的文本,我已经设置为像“没有加载”这样的东西,并且我得到了回调,但窗口小部件文本没有更新。

我已经搜遍了android文档以及堆栈溢出,但无法找到答案。我想也许我的ComponentName是不正确的,所以我只是在我的小部件管理器中获取小部件数组并对它们进行迭代。它确实得到小部件的列表,这似乎是正确的...

我做错了什么?

非常感谢, 猪

public class MistakeArchiveWidgetProvider extends AppWidgetProvider 
{ 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) 
    { 
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 60000); 

    // When the widget is clicked, launch the app. 
    for (int i = 0; i < appWidgetIds.length; i++) 
    { 
     int appWidgetId = appWidgetIds[i]; 

     Intent intent = new Intent(context, QuizActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     // Get the layout for the App Widget and attach an on-click listener 
     // to the button 
     RemoteViews views = new RemoteViews(context.getPackageName(), 
      R.layout.mistake_archive_widget); 
     views.setOnClickPendingIntent(R.id.STATIC_CUE, pendingIntent); 
     views.setOnClickPendingIntent(R.id.STATIC_ANSWER, pendingIntent); 

     // Tell the AppWidgetManager to perform an update on the current app widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
    } 

    private class MyTime extends TimerTask 
    { 
    public MyTime(Context context, AppWidgetManager appWidgetManager) 
    { 
     this.m_appWidgetManager = appWidgetManager; 
     m_remoteViews = new RemoteViews(context.getPackageName(), 
      R.layout.mistake_archive_widget); 

     m_componentName = new ComponentName(context, MistakeArchiveWidgetProvider.class); 
     m_context = context; 
    } 

    @Override 
    public void run() 
    { 
     MistakeArchive archive = new MistakeArchive(); 
     if(archive.restoreState(m_context)) 
     { 
     SharedPreferences prefs = 
      PreferenceManager.getDefaultSharedPreferences(m_context); 

     int activeIndex = prefs.getInt(
      MyPrefs.PREFKEY_ACTIVE_MISTAKE_ARCHIVE_HASH_INDEX, 0); 

     activeIndex++; 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.putInt(MyPrefs.PREFKEY_ACTIVE_MISTAKE_ARCHIVE_HASH_INDEX, 
      activeIndex); 
     editor.commit(); 

     CueLine cueLine = archive.getMistake(activeIndex); 
     if(cueLine != null) 
     { 
      m_remoteViews.setTextViewText(R.id.STATIC_CUE, cueLine.GetCue()); 
      m_remoteViews.setViewVisibility(R.id.STATIC_CUE, View.VISIBLE); 
      m_remoteViews.setTextViewText(R.id.STATIC_ANSWER, cueLine.GetAnswer()); 

      int[] appWidgetIds = m_appWidgetManager.getAppWidgetIds(m_componentName); 
      for (int i = 0; i < appWidgetIds.length; i++) 
      { 
      m_appWidgetManager.updateAppWidget(i, m_remoteViews); 
      } 
     } 
     } 
    } 

    RemoteViews m_remoteViews; 
    AppWidgetManager m_appWidgetManager; 
    ComponentName m_componentName; 
    Context m_context; 
    } 
} 

在我的清单XML我有:

<receiver android:name="myApp.MistakeArchiveWidgetProvider" android:label="@string/IDST_APP_NAME"> 
    <intent-filter> 
     <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
    </intent-filter> 
    <meta-data android:name="android.appwidget.provider" 
      android:resource="@xml/mistake_archive_widget_provider" /> 
</receiver> 

mistake_archive_widget_provider.xml:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="50dip" 
    android:minHeight="50dip" 
    android:updatePeriodMillis="10000" 
    android:initialLayout="@layout/mistake_archive_widget" 
/> 

mistake_archive_widget.xml:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/widget_background" 
    > 

     <TextView 
      android:id="@+id/STATIC_CUE" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:layout_weight="1" 
      android:textStyle="bold" 
      android:visibility="gone" 
      android:text="" 
      /> 

     <TextView android:id="@+id/STATIC_ANSWER" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center" 
        android:layout_weight="1" 
        android:text="@string/IDST_NO_CUES_LOADED" 
        /> 

</LinearLayout> 
+0

你的部件采取的单击事件或不? –

+0

是的点击事件的作品,但不是更新。 – swinefeaster

+0

只是确保您注册自定义接收器更新您的小部件? –

回答

0

好吧我想通了:不要使用TimerTask。这不符合预期。我很确定我在某个地方跟随一个例子,告诉我这样做,但第二个想法是,它看起来有缺陷。

我改变了一切,以便它在onUpdate()中完成,并删除了所有计时器的东西。 Android会以每30分钟的最大速率调用此函数。

现在

这么简单太:)

SOLUTION:

public class MistakeArchiveWidgetProvider extends AppWidgetProvider 
{ 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) 
    { 
    // When the widget is clicked, launch the app. 
    for (int widgetIndex = 0; widgetIndex < appWidgetIds.length; widgetIndex++) 
    { 
     int appWidgetId = appWidgetIds[widgetIndex]; 

     RemoteViews views = new RemoteViews(context.getPackageName(), 
      R.layout.mistake_archive_widget); 

     // Get the layout for the App Widget and attach an on-click listener. 
     { 
     Intent intent = new Intent(context, QuizActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

     views.setOnClickPendingIntent(R.id.STATIC_CUE, pendingIntent); 
     views.setOnClickPendingIntent(R.id.STATIC_ANSWER, pendingIntent); 
     } 

     views.setTextViewText(R.id.STATIC_CUE, cueString); 
     views.setTextViewText(R.id.STATIC_ANSWER, cueAnswer); 

     // Tell the AppWidgetManager to perform an update on the current app widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
    } 
} 
相关问题