Mega Code Archive
Categories
/
Android
/
Hardware
Using location service for the weather
/*** Copyright (c) 2008-2009 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.commonsware.android.internet; import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.webkit.WebView; import android.widget.Toast; import java.io.StringReader; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.client.ResponseHandler; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class WeatherDemo extends Activity { private LocationManager mgr=null; private String format; private WebView browser; private HttpClient client; private List
forecasts=new ArrayList
(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); mgr=(LocationManager)getSystemService(Context.LOCATION_SERVICE); format=getString(R.string.url); browser=(WebView)findViewById(R.id.webkit); client=new DefaultHttpClient(); } @Override public void onResume() { super.onResume(); mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10000.0f, onLocationChange); } @Override public void onPause() { super.onPause(); mgr.removeUpdates(onLocationChange); } private void updateForecast(Location loc) { String url=String.format(format, loc.getLatitude(), loc.getLongitude()); HttpGet getMethod=new HttpGet(url); try { ResponseHandler
responseHandler=new BasicResponseHandler(); String responseBody=client.execute(getMethod, responseHandler); buildForecasts(responseBody); String page=generatePage(); browser.loadDataWithBaseURL(null, page, "text/html", "UTF-8", null); } catch (Throwable t) { android.util.Log.e("WeatherDemo", "Exception fetching data", t); Toast .makeText(this, "Request failed: "+t.toString(), 4000) .show(); } } void buildForecasts(String raw) throws Exception { DocumentBuilder builder=DocumentBuilderFactory .newInstance() .newDocumentBuilder(); Document doc=builder.parse(new InputSource(new StringReader(raw))); NodeList times=doc.getElementsByTagName("start-valid-time"); for (int i=0;i
"); bufResult.append("
Time
"+ "
Temperature
Forecast
"); for (Forecast forecast : forecasts) { bufResult.append("
"); bufResult.append(forecast.getTime()); bufResult.append("
"); bufResult.append(forecast.getTemp()); bufResult.append("
"); } bufResult.append("