Reputation: 5904
I can't have the weather info withot gps.. The code to find my location and weather is this:
private boolean isRuntimePostGingerbread() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}
public void loadWeathercard() {
//Set the date
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
citylb = (TextView) findViewById(R.id.citybox);
//Check if the custom weather place preference is enabled
if (!sharedPrefs.getBoolean("weather_customlocationcheck", false)) {
citylb.setText(getString(R.string.rilevamento));
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
/**
* Check the provider exists before registering for updates from it
* */
if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, this);
if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, this);
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} else {
//Get the custom city, refresh the city label and get the weather
citylb.setText(sharedPrefs.getString("weather_customlocation", getString(R.string.location_null)));
getWeather((String) citylb.getText());
}
}
/**
* Function to check if the device is online
*/
/**
* Default method
*/
public Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected()){
return true;
// This is method suggested.. Have i use this?
}
return false;
}
private void getWeather(String location) {
if (isOnline()) {
Log.d("YWeatherGetter4a", "onCreate");
YahooWeatherUtils yahooWeatherUtils = YahooWeatherUtils.getInstance();
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), location, this); // Basta mettere la location presa dalle preferences
} else
Toast.makeText(getApplicationContext(), "Sorry, no connection available", Toast.LENGTH_SHORT).show();
}
/**
* Rilevo la posizione attuale
* */
public void gotWeatherInfo(final WeatherInfo weatherInfo) {
if (weatherInfo != null) {
current = weatherInfo.getCurrentText().toString();
// Converto in stringa la temperatura
TempCurrent = String.valueOf(weatherInfo.getCurrentTempC());
String TempHigh = String.valueOf(weatherInfo.getForecast1TempHighC());
String TempLow = String.valueOf(weatherInfo.getForecast1TempLowC());
// + weatherInfo.getAtmosphereHumidity() + "%" per l'umidit
/* LoadWebImagesTask task = new LoadWebImagesTask();
task.execute(
weatherInfo.getCurrentConditionIconURL()
);*/
Time oggi = new Time(Time.getCurrentTimezone());
oggi.setToNow();
String orariometeo = oggi.format("%k:%M");
ImageView imgView = (ImageView)findViewById(R.id.imageview_forecast_info);
if(current.toLowerCase().contains("sunny")){
current = getResources().getString(R.string.sunny);
Drawable myDrawable = getResources().getDrawable(R.drawable.sunny);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("partly cloudy")) {
current = getResources().getString(R.string.partlycloudy);
Drawable myDrawable = getResources().getDrawable(R.drawable.partlycloudy);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("mostly cloudy")) {
current = getResources().getString(R.string.cloudy);
Drawable myDrawable = getResources().getDrawable(R.drawable.mostlycloudy);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("fair")) {
current = getResources().getString(R.string.fair);
Drawable myDrawable = getResources().getDrawable(R.drawable.clear);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("showers")) {
current = getResources().getString(R.string.showers);
Drawable myDrawable = getResources().getDrawable(R.drawable.rain);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("foggy")) {
current = getResources().getString(R.string.foggy);
Drawable myDrawable = getResources().getDrawable(R.drawable.mist);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("thunderstorms")) {
current = getResources().getString(R.string.thunderstorms);
Drawable myDrawable = getResources().getDrawable(R.drawable.thunderstorm);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("thundershowers")) {
current = getResources().getString(R.string.thundershowers);
Drawable myDrawable = getResources().getDrawable(R.drawable.storm);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("freezingdrizzle") || current.toLowerCase().contains("hail")) {
current = getResources().getString(R.string.freezingdrizzle);
Drawable myDrawable = getResources().getDrawable(R.drawable.freezingdrizzle);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("lightsnowshowers")) {
current = getResources().getString(R.string.lightsnowshowers);
Drawable myDrawable = getResources().getDrawable(R.drawable.chanceofsnow);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("clear") || current.toLowerCase().contains("hot")) {
current = getResources().getString(R.string.clear);
Drawable myDrawable = getResources().getDrawable(R.drawable.clear);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("cloudy")) {
current = getResources().getString(R.string.cloudy);
Drawable myDrawable = getResources().getDrawable(R.drawable.cloudy);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("snow")) {
current = getResources().getString(R.string.snow);
Drawable myDrawable = getResources().getDrawable(R.drawable.snow);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("drizzle")) {
current = getResources().getString(R.string.drizzle);
Drawable myDrawable = getResources().getDrawable(R.drawable.lightrain);
imgView.setImageDrawable(myDrawable);
} else if (current.toLowerCase().contains("rain") || current.toLowerCase().contains("showers")) {
current = getResources().getString(R.string.showers);
Drawable myDrawable = getResources().getDrawable(R.drawable.rain);
imgView.setImageDrawable(myDrawable);
}
temperaturenow = (TextView) findViewById(R.id.temperaturenow);
temperaturenow.setText(current + " ");
temperatura = (TextView)findViewById(R.id.temperatura);
temperatura.setText(TempCurrent + "°");
maxmin = (TextView)findViewById(R.id.maxmin);
maxmin.setText(Html.fromHtml(TempHigh + "/" +"<small>"+TempLow+"</small>" +"°C"));
imgView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
boolean installed = appInstalledOrNot("com.yahoo.mobile.client.android.weather");
if(installed) {
String meteoinfo = weatherInfo.getWeatherurl();
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.yahoo.mobile.client.android.weather");
LaunchIntent.putExtra(SearchManager.QUERY, meteoinfo);
startActivity(LaunchIntent);
} else {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(weatherInfo.getWeatherurl()));
startActivity(i);
}
}
});
} else {
Toast.makeText(getApplicationContext(), "Sorry, no result returned", Toast.LENGTH_SHORT).show();
}
}
class LoadWebImagesTask extends AsyncTask<String, Void, Bitmap[]> {
@Override
protected Bitmap[] doInBackground(String... params) {
Bitmap[] res = new Bitmap[6];
res[0] = ImageUtils.getBitmapFromWeb(params[0]);
return res;
}
@Override
protected void onPostExecute(Bitmap[] results) {
super.onPostExecute(results);
citylb.setCompoundDrawablesWithIntrinsicBounds(null, null, null, new BitmapDrawable(getResources(), results[0]));
}
}
public void loadWeathercard1() {
//Set the date
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
citylbpref = (TextView) findViewById(R.id.cityboxpref);
//Check if the custom weather place preference is enabled
//Get the custom city, refresh the city label and get the weather
citylbpref.setText(sharedPrefs.getString("weather_customlocation", getString(R.string.location_null)));
getWeather1((String) citylbpref.getText());
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
List<Address> addresses = null;
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("MyTag", "MyMessage", e); // use this instead e.printStackTrace();
}
if(addresses != null && addresses.size() > 0){
citylb.setText(addresses.get(0).getLocality());
getWeather((String) citylb.getText());
}else{
citylb.setText(getString(R.string.location_null));
}
//citylb = (TextView) findViewById(R.id.citybox);
//if (addresses.size() > 0) return addresses.get(0).getLocality();
}
}
Now, what i need it's have a "manual" location.. I tried to change the "loation" here:
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), location, this);
using a string like:
yahooWeatherUtils.queryYahooWeather(getApplicationContext(), "New York", this);
but doesn't work... any idea?
Upvotes: 1
Views: 169
Reputation: 5651
If you use Googleplay services you can get your location without a GPS. It seems to default to sources such as cell towers and known wifi locations. Bonus if the device actually has a gps and the gps is turned on google play services will use it but it won't do the annoying please turn on gps so this will work perfectly fine for your application. The first fix is really fast too.
public class KmlReader extends ActionBarActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener
{
...
if (locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addGpsStatusListener(this);
}
* Called by Location Services when the request to connect the client
* finishes successfully. At this point, you can request the current
* location or start periodic updates
*/
@Override
public void onConnected(Bundle dataBundle) {
if (action_track) {
if (mLocationRequest == null) {
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
// Set the update interval
mLocationRequest.setInterval(UPDATE_INTERVAL);
// Use high accuracy
mLocationRequest
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
}
mLocationClient.requestLocationUpdates(mLocationRequest, this);
} else {
stopLocationUpdates();
}
}
and magically the location comes into this method.
@Override
public void onLocationChanged(Location location) {
Upvotes: 0
Reputation: 14376
Searching for YahooWeatherUtils, I found http://www.codota.com/android/scenarios/52fcbdbbda0ae4ff7532748f/com.google.android.gms.location.LocationClient?tag=dragonfly
Their suggestion is:
Location location = locationClient.getLastLocation();
YahooWeatherUtils ywu = YahooWeatherUtils.getInstance();
if PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean("weatherUseCurrentLocation", true) && location != null) {
ywu.queryYahooWeather(getActivity(), location , this);
} else {
String city = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("weatherCity", "Vancouver Canada");
ywu.queryYahooWeather(getActivity(), city , this);
}
So, it seems that the function queryYahooWeather
can get lat/long or city name. You just need enough information for city name.
Upvotes: 1