Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:55:59+00:00 2026-06-04T17:55:59+00:00

I am trying to make a simple widget which slowly rotates/iterates over a set

  • 0

I am trying to make a simple widget which slowly rotates/iterates over a set of cue cards that the user had trouble with (similar to a windows phone live tile).

I am able to create the widget and it shows the default text which I have set to say something like “Nothing loaded”, and I am getting callbacks, but the widget text is not updated.

I have scoured through the android docs as well as stack overflow but can’t find the answer. I thought maybe my ComponentName was incorrect, so I just get the array of widgets in my widget manager and iterate over them. It does get the list of widgets, so that seems correct…

What am I doing wrong?

Many thanks,
Swine

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

in my manifest xml i have:

 <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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:56:00+00:00Added an answer on June 4, 2026 at 5:56 pm

    Ok I figured it out: do not use TimerTask. This does not work as expected. I’m pretty sure I was following an example somewhere that told me to do it this way, but on second thought, it looks flawed.

    I changed everything so that it is done in the onUpdate(), and removed all the timer stuff. Android will call this function at a maximum rate of every 30 minutes.

    So simple now too 🙂

    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);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new in all widget staff so I'm trying to make one simple that
I am fairly new to iOS development and trying make a simple app which
I am trying to make a simple Activity which has a menu with two
I'm trying to make a real simple app in which clicking a button has
I'm trying to make a simple order system. So the user selects a burger
I'm trying to write a simple dashboard widget (possibly an additional webapp) that displays
I'm trying to make work a self-updating functionality in an Android widget, something simple
Trying to make a simple scroll to top button that smoothly scrolls up and
I am trying to make a simple HTTP server that would be able to
I'm trying to make a simple settings script, which basically send a setting name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.