2012-05-28 3 views
0

사용자가 문제가있는 큐 카드 세트를 천천히 회전/반복하는 간단한 위젯을 만들려고합니다 (Windows 전화 라이브 타일과 비슷 함).내 위젯이 업데이트되지 않는 이유는 무엇입니까?

위젯을 만들 수 있으며 "로드 된 항목 없음"과 같은 말을 설정 한 기본 텍스트가 표시되며 콜백이 표시되지만 위젯 텍스트는 업데이트되지 않습니다.

스택 오버플로뿐 아니라 안드로이드 도큐를 샅샅이 뒤졌지만 대답을 찾을 수 없습니다. 아마도 내 ComponentName이 올바르지 않을 것 같아서 위젯 관리자의 위젯 배열을 가져 와서 반복 해 보았습니다. 그것은 위젯의 목록을 얻을, 그래서 올바른 것 같습니다 ...

내가 뭘 잘못하고있어?

많은 감사합니다, 내 매니페스트 XML에 돼지

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; 
    } 
} 

난이 :

<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 :

,536,
<?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

예 click 이벤트는 작동하지만 업데이트는 작동하지 않습니다. – swinefeaster

+0

위젯을 업데이트하기 위해 맞춤 수신기를 등록했는지 확인하십시오. –

답변

0

좋습니다. 알아 냈습니다. TimerTask를 사용하지 마십시오. 예상대로 작동하지 않습니다. 나는 내가이 방법으로 그렇게 말한 어딘가의 예를 따르고 있다고 확신하지만, 두 번째 생각에는 결함이있는 것처럼 보인다.

모든 것이 바뀌어서 onUpdate()에서 완료되고 모든 타이머 항목이 제거되었습니다. Android는 최대 30 분마다이 기능을 호출합니다. 지금

그래서 간단한 너무 :

해결책 :

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); 
    } 
    } 
} 
관련 문제