|
|
@@ -0,0 +1,158 @@
|
|
|
+package kr.co.zumo.app.lifeplus.activity;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.location.Address;
|
|
|
+import android.location.Geocoder;
|
|
|
+import android.location.Location;
|
|
|
+import android.location.LocationListener;
|
|
|
+import android.location.LocationManager;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.annotation.Nullable;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.util.Log;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TestActivity
|
|
|
+ * <pre>
|
|
|
+ * </pre>
|
|
|
+ *
|
|
|
+ * @author 하세미
|
|
|
+ * @version 1.0
|
|
|
+ * @history 하세미 [2018-11-06] [최초 작성]
|
|
|
+ * @since 2018-11-06
|
|
|
+ */
|
|
|
+public class LocationActivity extends AppCompatActivity{
|
|
|
+
|
|
|
+ private final int REQUEST_CODE_PERMISSIONS=1000;
|
|
|
+ private LocationManager locationManager;
|
|
|
+ private LocationListener locationListener;
|
|
|
+
|
|
|
+ private boolean isGPSEnabled;
|
|
|
+ private boolean isNetworkEnabled;
|
|
|
+ private double latitude;
|
|
|
+ private double longitude;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setting();
|
|
|
+ getLocation();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void setting() {
|
|
|
+ locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
|
|
|
+ isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
|
|
|
+ isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
|
|
|
+
|
|
|
+ Log.e("APP# LocationActivity | onCreate", "|" + isGPSEnabled + ", " + isNetworkEnabled);
|
|
|
+
|
|
|
+ locationListener = new LocationListener() {
|
|
|
+ @Override
|
|
|
+ public void onLocationChanged(Location location) {
|
|
|
+ latitude = location.getLatitude();
|
|
|
+ longitude = location.getLongitude();
|
|
|
+ Log.e("APP# LocationActivity | onLocationChanged", "|" + latitude + ", " + longitude);
|
|
|
+ String address = getAddress(latitude, longitude);
|
|
|
+ Log.e("APP# LocationActivity | onLocationChanged", "|" + address);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onStatusChanged(String s, int i, Bundle bundle) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderEnabled(String s) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProviderDisabled(String s) {
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ switch (requestCode) {
|
|
|
+ case REQUEST_CODE_PERMISSIONS:
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ Toast.makeText(this, "권한 체크 거부됨", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getLocation() {
|
|
|
+
|
|
|
+ Location location = null;
|
|
|
+
|
|
|
+ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (isNetworkEnabled) {
|
|
|
+ locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 1, locationListener);
|
|
|
+ if (null != locationManager) {
|
|
|
+ location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
|
+ if (null != location) {
|
|
|
+ Log.e("APP# LocationActivity | getLocation", "|" + location.getLatitude() + ", " + location.getLongitude());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isGPSEnabled) {
|
|
|
+ if (location == null) {
|
|
|
+ locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, locationListener);
|
|
|
+ if (null != locationManager) {
|
|
|
+ location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
|
+ if (null != location) {
|
|
|
+ Log.e("APP# LocationActivity | getLocation", "|vv" + location.getLongitude() + ", " + location.getLatitude());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String getAddress(double latitude, double longitude){
|
|
|
+
|
|
|
+ String nowAddress ="none";
|
|
|
+ Geocoder geocoder = new Geocoder(this, Locale.KOREA);
|
|
|
+ List<Address> addressList;
|
|
|
+
|
|
|
+ if(null != geocoder){
|
|
|
+ try {
|
|
|
+ addressList = geocoder.getFromLocation(latitude, longitude, 1);
|
|
|
+ if(addressList != null && addressList.size() > 0){
|
|
|
+ String currentAddress = addressList.get(0).getAddressLine(0).toString();
|
|
|
+ nowAddress = currentAddress;
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nowAddress;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|