/** Fetches and displays the weather data from colorado_alarm_api.herokuapp.com */ private void loadWeatherData() { CustomJsonRequest request = new CustomJsonRequest( Request.Method.GET, API_ENDPOINT, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // if you want to debug: Log.v(getString(R.string.app_name), response.toString()); try { String precipType, temp, precipProbability, summary; summary = response.getString("summary"); temp = response.getString("temperature"); precipType = response.getString("precip_type"); precipProbability = response.getString("precip_probability"); mTxtDegrees.setText(temp + "degrees"); mTxtWeather.setText(summary); } catch (Exception e) { txtError(e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { txtError(error); } }); request.setPriority(Request.Priority.HIGH); helper.add(request); }
/** stops foreground threaded volley requests if the app is stopped */ @Override protected void onStop() { super.onStop(); // This will tell to Volley to cancel all the pending requests helper.cancel(); }
/** * Primary Activity for our app. Our "home screen". Shows the weather as well as the button which * allows for the alarm to be changed. */ public class MainActivity extends Activity { ImageView mImageView; TextView mTxtDegrees, mTxtWeather, mTxtError; RestHelper helper = RestHelper.getInstance(); int today = Calendar.getInstance().get(Calendar.DAY_OF_MONTH); int mainColor = Color.parseColor("#FF5722"); SharedPreferences mSharedPref; static final String API_ENDPOINT = "http://colorado-alarm-api.herokuapp.com/forecasts?lat=-105.2705456&lon=40.0149856"; /** * Binds views and retrieves weather from Heroku API * * @param savedInstanceState the application state that may still exists if this app was pushed to * the background */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Views setup mImageView = (ImageView) findViewById(R.id.main_bg); mTxtDegrees = (TextView) findViewById(R.id.degrees); mTxtWeather = (TextView) findViewById(R.id.weather); mTxtError = (TextView) findViewById(R.id.error); // Font mTxtDegrees.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Lato-Light.ttf")); mTxtWeather.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Lato-Light.ttf")); // SharedPreferences setup mSharedPref = getPreferences(Context.MODE_PRIVATE); // Weather data loadWeatherData(); // scheduleWeatherCheck(); } /** stops foreground threaded volley requests if the app is stopped */ @Override protected void onStop() { super.onStop(); // This will tell to Volley to cancel all the pending requests helper.cancel(); } /** Fetches and displays the weather data from colorado_alarm_api.herokuapp.com */ private void loadWeatherData() { CustomJsonRequest request = new CustomJsonRequest( Request.Method.GET, API_ENDPOINT, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { // if you want to debug: Log.v(getString(R.string.app_name), response.toString()); try { String precipType, temp, precipProbability, summary; summary = response.getString("summary"); temp = response.getString("temperature"); precipType = response.getString("precip_type"); precipProbability = response.getString("precip_probability"); mTxtDegrees.setText(temp + "degrees"); mTxtWeather.setText(summary); } catch (Exception e) { txtError(e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { txtError(error); } }); request.setPriority(Request.Priority.HIGH); helper.add(request); } private void imageError(Exception e) { mImageView.setBackgroundColor(mainColor); e.printStackTrace(); } /** * Utility function to keep error handling more dry * * @param e exception from text operations */ private void txtError(Exception e) { mTxtError.setVisibility(View.VISIBLE); e.printStackTrace(); } // UI Method /** * Callback for UI to change to the alarm management screen * * @param view view object passed in template */ public void setAlarm(View view) { // Build our Intent Intent intent = new Intent(this, AlarmActivity.class); // Don't need to pass anything, just transition. startActivity(intent); } // Setup a recurring alarm every half hour // public void scheduleWeatherCheck() { // // Construct an intent that will execute the AlarmReceiver // Intent intent = new Intent(getApplicationContext(), WeatherPullReceiver.class); // // Create a PendingIntent to be triggered when the alarm goes off // final PendingIntent pIntent = PendingIntent.getBroadcast(this, // WeatherPullReceiver.REQUEST_CODE, // intent, PendingIntent.FLAG_UPDATE_CURRENT); // // Setup periodic alarm every 5 seconds // long firstMillis = System.currentTimeMillis(); // alarm is set right away // AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, // AlarmManager.INTERVAL_HALF_HOUR, pIntent); // } // // public void cancelWeatherService() { // Intent intent = new Intent(getApplicationContext(), WeatherPullReceiver.class); // final PendingIntent pIntent = PendingIntent.getBroadcast(this, // WeatherPullReceiver.REQUEST_CODE, // intent, PendingIntent.FLAG_UPDATE_CURRENT); // AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // alarm.cancel(pIntent); // } }