public
class
HHRS_main
extends
Activity {
private
static
final
String TAG = HHRS_main.
class
.getName();
private
TextView textlocation;
private
TextView textAddress;
LocationManager locationManager;
LocationListener locationListener;
Location location;
Geocoder geoCoder;
String messageAddress =
"주소를 확인할 수 없음."
;
String messageLocation =
"위치를 확인할 수 없음."
;
double
latitude;
double
longitude;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setLayout();
myLocation();
}
/** 현재 위치 정보 시작 */
private
void
myLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
location = locationManager.getLastKnownLocation(provider);
latitude = location.getLatitude();
longitude = location.getLongitude();
messageLocation = String
.format(
"위도 : %f, 경도: %f"
, latitude, longitude);
if
(location !=
null
) {
textlocation.setText(messageLocation);
textAddress.setText(getAddres());
}
else
{
textAddress.setText(
"주소정보를 얻지 못함."
);
}
locationListener =
new
LocationListener() {
@Override
public
void
onStatusChanged(String provider,
int
status,
Bundle extras) {
}
@Override
public
void
onProviderEnabled(String provider) {
}
@Override
public
void
onProviderDisabled(String provider) {
}
@Override
public
void
onLocationChanged(Location location) {
textlocation.setText(messageLocation);
textAddress.setText(getAddres());
}
};
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,
0
,
0
, locationListener);
}
private
String getAddres() {
Geocoder gc =
new
Geocoder(getApplicationContext(), Locale.KOREA);
try
{
List<Address> addresses = gc
.getFromLocation(latitude, longitude,
1
);
StringBuilder sb =
new
StringBuilder();
if
(
null
!= addresses && addresses.size() >
0
) {
Address address = addresses.get(
0
);
sb.append(address.getLocality() +
" "
);
sb.append(address.getSubLocality() +
" "
);
sb.append(address.getThoroughfare() +
" "
);
sb.append(address.getFeatureName());
messageAddress = sb.toString();
}
}
catch
(IOException e) {
Toast.makeText(HHRS_main.
this
,
"주소를 얻지 못함"
,
1000
).show();
}
return
messageAddress;
}
/** 현재 위치 정보 끝 */
/** 레이아웃 시작 */
private
void
setLayout() {
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec(
"1"
);
spec.setContent(R.id.tab1);
spec.setIndicator(
"행동정보"
);
tabHost.addTab(spec);
spec = tabHost.newTabSpec(
"2"
);
spec.setContent(R.id.tab2);
spec.setIndicator(
"행동요량"
);
tabHost.addTab(spec);
spec = tabHost.newTabSpec(
"3"
);
spec.setContent(R.id.tab3);
spec.setIndicator(
"설정"
);
tabHost.addTab(spec);
textlocation = (TextView) findViewById(R.id.main_location);
textAddress = (TextView) findViewById(R.id.main_address);
}
/** 레이아웃 끝 */
}