Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

android - AppWidget alarmmanager not updating

So, I've got a widget, I want it to update every 60 seconds. When the widget is first added to the homescreen, it runs its update function just fine. Beyond that it's supposed to start an AlarmManager, which will rerun the update method every 60 seconds. That's the part that it doesn't seem to be actually doing. Here's my code:

public class ClockWidget extends AppWidgetProvider {

    public static String CLOCK_WIDGET_UPDATE = "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) {
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clocklayout);
            appWidgetManager.updateAppWidget(appWidgetId, views);
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        Log.d("log","Entered update cycle");
        //Unimportant for these purposes
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }


    private PendingIntent createClockTickIntent(Context context) {
        Intent intent = new Intent(CLOCK_WIDGET_UPDATE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.d("onEnabled","Widget Provider enabled.  Starting timer to update widget every minute");
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), 60000, createClockTickIntent(context));
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.d("onDisabled", "Widget Provider disabled. Turning off timer");
        AlarmManager alarmManager = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(createClockTickIntent(context));
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d("onReceive", "Received intent " + intent);
        if (CLOCK_WIDGET_UPDATE.equals(intent.getAction())) {
            Log.d("onReceive", "Clock update");
            // Get the widget manager and ids for this widget provider, then
            // call the shared
            // clock update method.
            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
            for (int appWidgetID: ids) {
                updateAppWidget(context, appWidgetManager, appWidgetID);
            }
        }
    }

}

It's the product of a few tutorials I've found on the matter, and my own knowledge of Android. According to my logcats, it never gets to the Log.d("onReceive", "Clock update"); line. And yes, my Manifest is set up with the clock update intent. Thanks!

EDIT: Additional info. I put a log line in the createClockTickIntent method, and it fires off. So I guess this means that my application is running the alarmManager.setRepeating line, no idea why is isn't actually repeating.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Arggghhh, it was a simple typo. The intent filter was "com.nickavv.cleanwidgets.CLEANCLOCK_UPDATE", and I had written "com.nickavv.cleanwidget.CLEANCLOCK_UPDATE"

What a pain, but hey, now I know.

So, moral of the story for anybody with a similar problem to me: Check your spelling! Check it twice, or ten times. On everything!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...