DeNitE Appz
DeNitE Appz

Reputation: 1011

Why does my Clock widget stop updating - using IntentFilter(Intent.ACTION_TIME_TICK)

I'm trying to create a clock widget that updates once every minute.

I tried using the AlarmManager to send update requests, but I figured that would not be as accurate as using IntentFilter(Intent.ACTION_TIME_TICK)

The widget loads perfectly, and updates for about 10 minutes or so using the ACTION_TIME_TICK IntentFilter... but then just stops updating.

Here's my AppWidgetProvider class

public class ClockWidget extends AppWidgetProvider
{
    private final String TAG = "ClockWidget";

    private static SharedPreferences sharedPrefs;
    private static SharedPreferences.Editor prefEditor;
    private final String SHARED_PREF_NAME = "ClassicBlack_Prefs";

    private BroadcastReceiver receiver;

    @Override
    public void onEnabled(Context context)
    {
        Log.d(TAG, "onEnabled()");

        super.onEnabled(context);
        sharedPrefs = context.getSharedPreferences(SHARED_PREF_NAME, 0);
        prefEditor = sharedPrefs.edit();
        prefEditor.putBoolean("isWatchfaceActive", true).commit();

        drawWatch(context);

        receiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context ctx, Intent intent)
            {
                if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
                {
                    drawWatch(ctx);
                }
            }
        };

        context.getApplicationContext().registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        Log.d(TAG, "onUpdate()");
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context)
    {
        Log.d(TAG, "onDisabled()");

        super.onDisabled(context);
        sharedPrefs = context.getSharedPreferences(SHARED_PREF_NAME, 0);
        prefEditor = sharedPrefs.edit();
        prefEditor.putBoolean("isWatchfaceActive", false).commit();

        if (receiver != null)
        {
            context.getApplicationContext().unregisterReceiver(receiver);
        }
    }

    private void drawWatch(Context context)
    {
        Log.d(TAG, "drawWatch()");

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
        ComponentName thisWidget = new ComponentName(context, ClockWidget.class);

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(allWidgetIds));

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < allWidgetIds.length; i++)
        {
            DrawWatchFace drawWatchFace = new DrawWatchFace(context, null, true, 500);
            drawWatchFace.createWatchWidget();

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_layout);
            views.setImageViewBitmap(R.id.widget_canvas_imageView_small, drawWatchFace.getPalletBitmap());

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

Any suggestions?

Upvotes: 2

Views: 557

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

Your process is being terminated. This is perfectly normal. When your process terminates, your receiver is destroyed, and you will no longer receive broadcasts.

You cannot register for ACTION_TIME_TICK in the manifest, which is the typical way of handling this.

I recommend that you go back to your AlarmManager approach.

Upvotes: 4

Related Questions