2016-11-24 99 views
0

我正在使用udacity的sunshine應用程序項目製作天氣應用程序,它根據用戶的當前位置更新天氣。我想將當前位置設置爲默認值,讓用戶在首選項中進行更改,如果用戶在首選項中沒有更改位置,它會默認檢測位置並進行設置。我正在使用位置管理器並在首選項中設置默認值。問題都有不同的範圍。這是代碼段的預測。訪問覆蓋函數中的變量並在其他函數中使用

public static String getPreferredLocation(Context context) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    return prefs.getString(context.getString(R.string.pref_location_key), 
      context.getString(R.string.pref_location_default)); 

} 

而這正是我使用LocationListener的獲取當前城市名稱

public void onLocationChanged(Location loc) { 
    String longitude = "Longitude: " + loc.getLongitude(); 
    Log.v(TAG, longitude); 
    String latitude = "Latitude: " + loc.getLatitude(); 
    Log.v(TAG, latitude); 
    String cityName = null; 
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); 
    List<Address> addresses; 
    try { 
     addresses = gcd.getFromLocation(loc.getLatitude(), 
       loc.getLongitude(), 1); 
     if (addresses.size() > 0) { 
      System.out.println(addresses.get(0).getLocality()); 
      cityName = addresses.get(0).getLocality(); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我想cityname.But更換pref_location_default我無法訪問onLoctionChanged function.I外城市名不能在優先級位置默認情況下使用它們。所有這些都在同一個類中實現,這裏是類的完整代碼。

Utility.java

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.location.Address; 
import android.location.Geocoder; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.text.format.Time; 
import android.util.Log; 
import java.io.IOException; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 
import java.util.Locale; 
import static android.content.ContentValues.TAG; 

public class Utility extends Activity implements LocationListener { 





public static final String DATE_FORMAT = "yyyyMMdd"; 

public static String getPreferredLocation(Context context) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    return prefs.getString(context.getString(R.string.pref_location_key), 
      context.getString(R.string.pref_location_default)); 

} 

public static boolean isMetric(Context context) { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    return prefs.getString(context.getString(R.string.pref_units_key), 
      context.getString(R.string.pref_units_metric)) 
      .equals(context.getString(R.string.pref_units_metric)); 
} 

public static String formatTemperature(Context context, double temperature) { 
    // Data stored in Celsius by default. If user prefers to see in Fahrenheit, convert 
    // the values here. 
    String suffix = "\u00B0"; 
    if (!isMetric(context)) { 
     temperature = (temperature * 1.8) + 32; 
    } 

    // For presentation, assume the user doesn't care about tenths of a degree. 
    return String.format(context.getString(R.string.format_temperature), temperature); 
} 

static String formatDate(long dateInMilliseconds) { 
    Date date = new Date(dateInMilliseconds); 
    return DateFormat.getDateInstance().format(date); 
} 

/** 
* Helper method to convert the database representation of the date into something to display 
* to users. As classy and polished a user experience as "20140102" is, we can do better. 
* 
* @param context  Context to use for resource localization 
* @param dateInMillis The date in milliseconds 
* @return a user-friendly representation of the date. 
*/ 
public static String getFriendlyDayString(Context context, long dateInMillis) { 
    // The day string for forecast uses the following logic: 
    // For today: "Today, June 8" 
    // For tomorrow: "Tomorrow" 
    // For the next 5 days: "Wednesday" (just the day name) 
    // For all days after that: "Mon Jun 8" 

    Time time = new Time(); 
    time.setToNow(); 
    long currentTime = System.currentTimeMillis(); 
    int julianDay = Time.getJulianDay(dateInMillis, time.gmtoff); 
    int currentJulianDay = Time.getJulianDay(currentTime, time.gmtoff); 

    // If the date we're building the String for is today's date, the format 
    // is "Today, June 24" 
    if (julianDay == currentJulianDay) { 
     String today = context.getString(R.string.today); 
     int formatId = R.string.format_full_friendly_date; 
     return String.format(context.getString(
       formatId, 
       today, 
       getFormattedMonthDay(context, dateInMillis))); 
    } else if (julianDay < currentJulianDay + 7) { 
     // If the input date is less than a week in the future, just return the day name. 
     return getDayName(context, dateInMillis); 
    } else { 
     // Otherwise, use the form "Mon Jun 3" 
     SimpleDateFormat shortenedDateFormat = new SimpleDateFormat("EEE MMM dd"); 
     return shortenedDateFormat.format(dateInMillis); 
    } 
} 

/** 
* Given a day, returns just the name to use for that day. 
* E.g "today", "tomorrow", "wednesday". 
* 
* @param context  Context to use for resource localization 
* @param dateInMillis The date in milliseconds 
* @return 
*/ 
public static String getDayName(Context context, long dateInMillis) { 
    // If the date is today, return the localized version of "Today" instead of the actual 
    // day name. 

    Time t = new Time(); 
    t.setToNow(); 
    int julianDay = Time.getJulianDay(dateInMillis, t.gmtoff); 
    int currentJulianDay = Time.getJulianDay(System.currentTimeMillis(), t.gmtoff); 
    if (julianDay == currentJulianDay) { 
     return context.getString(R.string.today); 
    } else if (julianDay == currentJulianDay + 1) { 
     return context.getString(R.string.tomorrow); 
    } else { 
     Time time = new Time(); 
     time.setToNow(); 
     // Otherwise, the format is just the day of the week (e.g "Wednesday". 
     SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE"); 
     return dayFormat.format(dateInMillis); 
    } 
} 

/** 
* Converts db date format to the format "Month day", e.g "June 24". 
* 
* @param context  Context to use for resource localization 
* @param dateInMillis The db formatted date string, expected to be of the form specified 
*      in Utility.DATE_FORMAT 
* @return The day in the form of a string formatted "December 6" 
*/ 
public static String getFormattedMonthDay(Context context, long dateInMillis) { 
    Time time = new Time(); 
    time.setToNow(); 
    SimpleDateFormat dbDateFormat = new SimpleDateFormat(Utility.DATE_FORMAT); 
    SimpleDateFormat monthDayFormat = new SimpleDateFormat("MMMM dd"); 
    String monthDayString = monthDayFormat.format(dateInMillis); 
    return monthDayString; 
} 

public static String getFormattedWind(Context context, float windSpeed, float degrees) { 
    int windFormat; 
    if (Utility.isMetric(context)) { 
     windFormat = R.string.format_wind_kmh; 
    } else { 
     windFormat = R.string.format_wind_mph; 
     windSpeed = .621371192237334f * windSpeed; 
    } 

    // From wind direction in degrees, determine compass direction as a string (e.g NW) 
    // You know what's fun, writing really long if/else statements with tons of possible 
    // conditions. Seriously, try it! 
    String direction = "Unknown"; 
    if (degrees >= 337.5 || degrees < 22.5) { 
     direction = "N"; 
    } else if (degrees >= 22.5 && degrees < 67.5) { 
     direction = "NE"; 
    } else if (degrees >= 67.5 && degrees < 112.5) { 
     direction = "E"; 
    } else if (degrees >= 112.5 && degrees < 157.5) { 
     direction = "SE"; 
    } else if (degrees >= 157.5 && degrees < 202.5) { 
     direction = "S"; 
    } else if (degrees >= 202.5 && degrees < 247.5) { 
     direction = "SW"; 
    } else if (degrees >= 247.5 && degrees < 292.5) { 
     direction = "W"; 
    } else if (degrees >= 292.5 && degrees < 337.5) { 
     direction = "NW"; 
    } 
    return String.format(context.getString(windFormat), windSpeed, direction); 
} 

/** 
* Helper method to provide the icon resource id according to the weather condition id returned 
* by the OpenWeatherMap call. 
* 
* @param weatherId from OpenWeatherMap API response 
* @return resource id for the corresponding icon. -1 if no relation is found. 
*/ 
public static int getIconResourceForWeatherCondition(int weatherId) { 
    // Based on weather code data found at: 
    // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes 
    if (weatherId >= 200 && weatherId <= 232) { 
     return R.drawable.ic_storm; 
    } else if (weatherId >= 300 && weatherId <= 321) { 
     return R.drawable.ic_light_rain; 
    } else if (weatherId >= 500 && weatherId <= 504) { 
     return R.drawable.ic_rain; 
    } else if (weatherId == 511) { 
     return R.drawable.ic_snow; 
    } else if (weatherId >= 520 && weatherId <= 531) { 
     return R.drawable.ic_rain; 
    } else if (weatherId >= 600 && weatherId <= 622) { 
     return R.drawable.ic_snow; 
    } else if (weatherId >= 701 && weatherId <= 761) { 
     return R.drawable.ic_fog; 
    } else if (weatherId == 761 || weatherId == 781) { 
     return R.drawable.ic_storm; 
    } else if (weatherId == 800) { 
     return R.drawable.ic_clear; 
    } else if (weatherId == 801) { 
     return R.drawable.ic_light_clouds; 
    } else if (weatherId >= 802 && weatherId <= 804) { 
     return R.drawable.ic_cloudy; 
    } 
    return -1; 
} 

/** 
* Helper method to provide the art resource id according to the weather condition id returned 
* by the OpenWeatherMap call. 
* 
* @param weatherId from OpenWeatherMap API response 
* @return resource id for the corresponding icon. -1 if no relation is found. 
*/ 
public static int getArtResourceForWeatherCondition(int weatherId) { 
    // Based on weather code data found at: 
    // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes 
    if (weatherId >= 200 && weatherId <= 232) { 
     return R.drawable.art_storm; 
    } else if (weatherId >= 300 && weatherId <= 321) { 
     return R.drawable.art_light_rain; 
    } else if (weatherId >= 500 && weatherId <= 504) { 
     return R.drawable.art_rain; 
    } else if (weatherId == 511) { 
     return R.drawable.art_snow; 
    } else if (weatherId >= 520 && weatherId <= 531) { 
     return R.drawable.art_rain; 
    } else if (weatherId >= 600 && weatherId <= 622) { 
     return R.drawable.art_snow; 
    } else if (weatherId >= 701 && weatherId <= 761) { 
     return R.drawable.art_fog; 
    } else if (weatherId == 761 || weatherId == 781) { 
     return R.drawable.art_storm; 
    } else if (weatherId == 800) { 
     return R.drawable.art_clear; 
    } else if (weatherId == 801) { 
     return R.drawable.art_light_clouds; 
    } else if (weatherId >= 802 && weatherId <= 804) { 
     return R.drawable.art_clouds; 
    } 
    return -1; 
} 

/** 
* Helper method to provide the art resource id according to the weather condition id returned 
* by the OpenWeatherMap call. 
* 
* @param weatherId from OpenWeatherMap API response 
* @return resource id for the corresponding icon. -1 if no relation is found. 
*/ 
public static int getColorRessourceForWeatherCondition(int weatherId) { 
    // Based on weather code data found at: 
    // http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes 
    return -1; 
} 


@Override 
public void onLocationChanged(Location loc) { 
    String longitude = "Longitude: " + loc.getLongitude(); 
    Log.v(TAG, longitude); 
    String latitude = "Latitude: " + loc.getLatitude(); 
    Log.v(TAG, latitude); 
    String cityName = null; 
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); 
    List<Address> addresses; 
    try { 
     addresses = gcd.getFromLocation(loc.getLatitude(), 
       loc.getLongitude(), 1); 
     if (addresses.size() > 0) { 
      System.out.println(addresses.get(0).getLocality()); 
      cityName = addresses.get(0).getLocality(); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

}

回答

0

你只需要在你的utility.java類中定義字符串作爲一個字段的方法之外,以便它可以在其他地方訪問你想要的。

你可以在方法外聲明它作爲

public String cityName;