에이치의 모바일 앱 개발
내 위치 정보 가져오기<기본> 본문
/**
* Created by lsh on 2016-08-12.
* version 1.0.1
*/
public class BaseLocation extends Service implements LocationListener {
// 단위 second
private static final int LOCATION_UPDATE_CYCLE_MIN_TIME = 1000 * 60;
// 단위 meter
private static final int LOCATION_UPDATE_MIN_DISTANCE = 10;
private double lat, lon;
private boolean isGPSEnabled, isNetworkEnabled, isLocationResult;
private Context context;
private Location location;
private LocationManager locationManager;
public BaseLocation(Context context) {
this.context = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
this.isLocationResult = false;
} else {
this.isLocationResult = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_CYCLE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
setLatLon(location.getLatitude(), location.getLongitude());
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_UPDATE_CYCLE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
setLatLon(location.getLatitude(), location.getLongitude());
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
public void setLatLon(double lat, double lon) {
Log.e("Test", "lat : " + String.valueOf(lat) + ", lon : " + String.valueOf(lon));
this.lat = lat;
this.lon = lon;
}
public double getLatitude() {
if (location != null) {
lat = location.getLatitude();
}
return lat;
}
public double getLongitude() {
if (location != null) {
lon = location.getLongitude();
}
return lon;
}
public boolean isLocationResult() {
return isLocationResult;
}/**
* GPS 정보 획득 실패 시 설정창 이동 확인 창
*/
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS 사용유무셋팅");
alertDialog.setMessage("GPS 셋팅을 위해 설정창으로 가시겠습니까?");
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
'Android > Android 개발 소스' 카테고리의 다른 글
ListView Hegiht Setting version 1.0.1 (0) | 2017.12.23 |
---|---|
keypad setting version 1.0.1 (0) | 2017.12.23 |
TextView> UI Postion Setting version 1.0.1 (0) | 2017.12.23 |
내 위치 정보 가져오기<확장> (0) | 2017.12.23 |
연결된 와이파이 정보 가져오기 (0) | 2017.12.22 |