LocationManager locationManager;
private
Context mContext;
boolean
isGPSEnabled =
false
;
boolean
isNetWorkEnabled =
false
;
boolean
isGetLocation =
false
;
Location location;
double
lat;
double
lon;
private
static
final
long
MIN_DISTANCE_CHANGE_FOR_UPDATES =
1000
;
private
static
final
long
MIN_TIME_BW_UPDATES =
1000
*
60
*
1
;
public
Location getLocation() {
try
{
locationManager = (LocationManager)
this
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetWorkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if
(!isGPSEnabled && !isNetWorkEnabled) {
}
else
{
this
.isGetLocation =
true
;
try
{
if
(isNetWorkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if
(locationManager !=
null
) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if
(location !=
null
) {
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
if
(isGPSEnabled) {
if
(location ==
null
) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener);
if
(locationManager !=
null
) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if
(location !=
null
) {
lat = location.getLatitude();
lon = location.getLongitude();
}
}
}
}
}
catch
(SecurityException e) {
Log.d(
"error"
,
"오류"
,e);
}
}
}
catch
(Exception e) {
e.printStackTrace();
}
return
location;
}
private
LocationListener locationListener =
new
LocationListener() {
@Override
public
void
onLocationChanged(Location location) {
getWeatherData(location.getLatitude(),location.getLongitude());
}
@Override
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {
}
@Override
public
void
onProviderEnabled(String provider) {
}
@Override
public
void
onProviderDisabled(String provider) {
}
};
private
void
getWeatherData (
double
lat,
double
lon){
"&lon="
+ lon +
"&units=metric&appid=081bed6a85d281e8a3b8f5d2903293f9"
;
ReceiveWeatherTask receiveWeatherTask =
new
ReceiveWeatherTask();
receiveWeatherTask.execute(url);
}
private
class
ReceiveWeatherTask
extends
AsyncTask<String, Void, JSONObject> {
@Override
protected
JSONObject doInBackground(String... params) {
try
{
HttpURLConnection conn = (HttpURLConnection)
new
URL(params[
0
]).openConnection();
conn.setConnectTimeout(
10000
);
conn.setReadTimeout(
10000
);
conn.connect();
if
(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
InputStreamReader reader =
new
InputStreamReader(is);
BufferedReader in =
new
BufferedReader(reader);
String readed;
while
((readed = in.readLine()) !=
null
) {
JSONObject jsonObject =
new
JSONObject(readed);
return
jsonObject;
}
}
else
{
return
null
;
}
return
null
;
}
catch
(Exception e) {
Log.d(
"Error"
,
"Cannot process JSON results"
,e);
}
return
null
;
}
@Override
protected
void
onPostExecute(JSONObject result) {
Log.i(
"error"
,result.toString());
if
(result !=
null
) {
String iconName=
""
;
String nowTemp=
""
;
String humidity=
""
;
String main =
""
;
String description =
""
;
String city=
""
;
String update=
""
;
try
{
iconName = result.getJSONArray(
"weather"
).getJSONObject(
0
).getString(
"icon"
);
nowTemp=result.getJSONObject(
"main"
).getString(
"temp"
);
humidity=result.getJSONObject(
"main"
).getString(
"humidity"
);
main=result.getJSONArray(
"weather"
).getJSONObject(
0
).getString(
"main"
);
description=result.getJSONArray(
"weather"
).getJSONObject(
0
).getString(
"description"
);
city=result.getString(
"name"
).toUpperCase(Locale.US) +
","
+result.getJSONObject(
"sys"
)
.getString(
"country"
);
DateFormat df = DateFormat.getDateInstance();
update =df.format(
new
Date(result.getLong(
"dt"
)*
1000
));
icon.setText(iconName);
temp.setText(nowTemp);
Humidity.setText(humidity);
Cityfield.setText(city);
Update.setText(update);
}
catch
(JSONException e) {
Log.d(
"error"
,
"오류"
,e);
}
}
}
}
}