Android Open Source - weather Weather Alarm Receiver






From Project

Back to project page weather.

License

The source code is released under:

MIT License

If you think the Android project weather 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

package au.com.codeka.weather;
//  w w w . j av  a 2s. co  m
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class WeatherAlarmReceiver extends BroadcastReceiver {
  private static final String TAG = WeatherAlarmReceiver.class.getSimpleName();

  /** This is called by the system when an alarm is received. */
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "alarm received.");

    try {
      // refresh the widget, because we'll have just switched over to a new minute
      WeatherWidgetProvider.notifyRefresh(context);
  
      // fetch weather, which may also update the widget as well...
      WeatherManager.i.refreshWeather(context, false);
    } catch (Exception e) {
      Log.e(TAG, "Exception caught processing alarm!", e);
    }

    // schedule the next alarm to run
    schedule(context);
  }

  /** Schedule the alarm to run just after the next minute ticks over. */
  public static void schedule(Context context) {
    // make sure the alarm is running
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, WeatherAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    long millisPerMinute = 1000 * 60;
    long nextMinute = System.currentTimeMillis();
    nextMinute = nextMinute - (nextMinute % millisPerMinute) + millisPerMinute;

    Log.d(TAG, "now="+System.currentTimeMillis()+" nextMinute="+nextMinute);
    ActivityLog.current().setMillisToNextAlarm(nextMinute - System.currentTimeMillis());

    // we must remember to call schedule() after processing the first intent
    alarmManager.set(AlarmManager.RTC, nextMinute,  pendingIntent);
  }
}




Java Source Code List

au.com.codeka.weather.ActivityLog.java
au.com.codeka.weather.DebugActivity.java
au.com.codeka.weather.GeocodeInfo.java
au.com.codeka.weather.OpenWeatherMapInfo.java
au.com.codeka.weather.WeatherActivity.java
au.com.codeka.weather.WeatherAlarmReceiver.java
au.com.codeka.weather.WeatherInfo.java
au.com.codeka.weather.WeatherManager.java
au.com.codeka.weather.WeatherWidgetProvider.java