private void sendBroadcast(WeatherInfo w) { Intent broadcast = new Intent(INTENT_WEATHER_UPDATE); w.timestamp = Helpers.getTimestamp(getApplicationContext()); try { broadcast.putExtra(EXTRA_CITY, w.city); broadcast.putExtra(EXTRA_CONDITION, w.condition); broadcast.putExtra(EXTRA_LAST_UPDATE, w.timestamp); broadcast.putExtra(EXTRA_CONDITION_CODE, w.condition_code); broadcast.putExtra(EXTRA_FORECAST_DATE, w.forecast_date); broadcast.putExtra(EXTRA_HUMIDITY, w.humidity); broadcast.putExtra(EXTRA_TEMP, w.temp); broadcast.putExtra(EXTRA_WIND, w.wind); broadcast.putExtra(EXTRA_LOW, w.low); broadcast.putExtra(EXTRA_HIGH, w.high); } catch (Exception e) { e.printStackTrace(); } getApplicationContext().sendBroadcast(broadcast); }
@Override protected void onHandleIntent(Intent intent) { WeatherInfo w = null; String extra = null; String action = intent.getAction(); String woeid = null; Context context = getApplicationContext(); if (Settings.System.getInt(getContentResolver(), Settings.System.USE_WEATHER, 0) == 0) { stopSelf(); return; } if (!Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.NETWORK_PROVIDER) && !WeatherPrefs.getUseCustomLocation(getApplicationContext())) { stopSelf(); return; } if (action != null && action.equals(INTENT_WEATHER_REQUEST)) { // custom location boolean useCustomLoc = WeatherPrefs.getUseCustomLocation(getApplicationContext()); String customLoc = WeatherPrefs.getCustomLocation(getApplicationContext()); boolean manual = false; Bundle extras = intent.getExtras(); if (extras != null) { manual = extras.getBoolean(INTENT_EXTRA_ISMANUAL, false); } if (customLoc != null && useCustomLoc) { if (manual) { makeToast(context.getString(R.string.weather_refreshing)); } woeid = YahooPlaceFinder.GeoCode(getApplicationContext(), customLoc); // network location } else { // do not attempt to get a location without data boolean networkAvailable = Helpers.isNetworkAvailable(getApplicationContext()); if (networkAvailable) { if (manual) { makeToast(context.getString(R.string.weather_refreshing)); } final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (!intent.hasExtra(INTENT_EXTRA_NEWLOCATION)) { intent.putExtra(INTENT_EXTRA_NEWLOCATION, true); PendingIntent pi = PendingIntent.getService( getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, pi); return; } Criteria crit = new Criteria(); crit.setAccuracy(Criteria.ACCURACY_COARSE); String bestProvider = locationManager.getBestProvider(crit, true); Location loc = null; if (bestProvider != null) { loc = locationManager.getLastKnownLocation(bestProvider); } else { loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); } try { woeid = YahooPlaceFinder.reverseGeoCode( getApplicationContext(), loc.getLatitude(), loc.getLongitude()); } catch (Exception e) { e.printStackTrace(); } } else { if (manual) { makeToast(context.getString(R.string.location_unavailable)); } stopSelf(); return; } } try { w = parseXml(getDocument(woeid)); if (w != null) { sendBroadcast(w); updateLatest(w); } } catch (Exception e) { Log.e(TAG, "ohnoes: " + e.getMessage()); } } stopSelf(); }