If you think the Android project worldclockwidget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2012 Armin Hberling/*fromwww.java2s.com*/
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/package ch.corten.aha.worldclock;
import java.util.ArrayList;
import java.util.List;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
publicabstractclass AbstractWeatherWidgetProvider extends ClockWidgetProvider {
privatestaticfinal List<Class<? extends AppWidgetProvider>> WEATHER_WIDGET_PROVIDERS = new ArrayList<Class<? extends AppWidgetProvider>>();
protectedstaticvoid registerWeatherWidget(Class<? extends AbstractWeatherWidgetProvider> provider) {
registerClockWidget(provider);
WEATHER_WIDGET_PROVIDERS.add(provider);
}
@Override
publicvoid onEnabled(Context context) {
super.onEnabled(context);
enableUpdateService(context);
}
@Override
publicvoid onDisabled(Context context) {
super.onDisabled(context);
if (!isAnyWidgetActive(context, WEATHER_WIDGET_PROVIDERS)) {
disableUpdateService(context);
}
}
/**
* Enable weather update service.
*/privatestaticvoid enableUpdateService(Context context) {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),
AlarmManager.INTERVAL_HOUR, createWeatherUpdateIntent(context));
}
/**
* Disable weather update service.
*/privatestaticvoid disableUpdateService(Context context) {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(createWeatherUpdateIntent(context));
}
privatestatic PendingIntent createWeatherUpdateIntent(Context context) {
Intent service = new Intent(context, UpdateWeatherService.class);
service.putExtra(UpdateWeatherService.BACKGROUND_UPDATE, true);
return PendingIntent.getService(context, 0, service, PendingIntent.FLAG_UPDATE_CURRENT);
}
}