package com.testgam.testgetlocation;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class TestGetLocationActivity extends Activity {
    /** Called when the activity is first created. */
    LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();

        startG();
    }

    private void init() {
        // TODO Auto-generated method stub
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }

    private boolean getGPSStatus() {
        String allowedLocationProviders = Settings.System.getString(
                getContentResolver(),
                Settings.System.LOCATION_PROVIDERS_ALLOWED);

        if (allowedLocationProviders == null) {
            allowedLocationProviders = "";
        }

        return allowedLocationProviders.contains(LocationManager.GPS_PROVIDER);
    }

    private void setGPSStatus(boolean pNewGPSStatus) {
        String allowedLocationProviders = Settings.System.getString(
                getContentResolver(),
                Settings.System.LOCATION_PROVIDERS_ALLOWED);

        if (allowedLocationProviders == null) {
            allowedLocationProviders = "";
        }

        boolean networkProviderStatus = allowedLocationProviders
                .contains(LocationManager.NETWORK_PROVIDER);

        allowedLocationProviders = "";
        if (networkProviderStatus == true) {
            allowedLocationProviders += LocationManager.NETWORK_PROVIDER;
        }
        if (pNewGPSStatus == true) {
            allowedLocationProviders += "," + LocationManager.GPS_PROVIDER;
        }

        Settings.System.putString(getContentResolver(),
                Settings.System.LOCATION_PROVIDERS_ALLOWED,
                allowedLocationProviders);

        
        return;
    }

    public void startG() {
        try {
            if (!getGPSStatus())
                setGPSStatus(true);
        } catch (Exception e) {
            Log.e("%s:%s", e.getClass().getName());
        }
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                sendLocationUpdate(location);
            }

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }

            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub

            }

            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }
        };
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                0, locationListener);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null) {
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                sendLocationUpdate(location);
            }
        }
    }

    public void sendLocationUpdate(Location location) {
        Toast.makeText(this, "Latitud: "+location.getLatitude()+
                "Longitude: "+location.getLongitude()+
                "\nAccuracy: "+location.getAccuracy()+
                "\nProvider: "+location.getProvider(), Toast.LENGTH_SHORT).show();
    }

}


인터넷을 참고하여 간단한 예제를 작성하여 실행해보았는데요.

일단 정확한 위치인지는 모르나 대략 Accuracy가 1700~1800정도의 위치 값을 가져오네요.

그런데 일단 처음 실행하면 요놈이 다섯번정도의 토스트가 연속으로 뜹니다.

그런데 백버튼을 눌러서 액티비티를 종료했는데도 일정한 주기로 토스트가 계속해서 뜨네요.


혹시 LocationListener locationListener = new LocationListener() {...}; 부분의 리스너라는게 시스템에서 관리하는건가요?

그래서 프로그램을 백키로 끄더라도 리스너에 의해서 계속해서 호출이 되는건가요??..

만약 그렇다면 LocationListener뿐만 아니라 다른 리스너도 프로그램 종료할때 해제 시켜줘야 하나요?

브로드캐스트 리시버도 아니고 왜 이러는건지..

방금 또 떴는데 한번 뜨네요..

당췌 모르겠습니다 ㅠ_ㅠ...


의심가는부분이라도 알려주시면 직접 알아보겠습니다. 감사합니다.



------------------------------

익스7, 크롬에서는 코드하이라이터가 한줄씩 쪼개지는데 파폭에선 되네요.