package com.example.ex_0428;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
import android.widget.Toast;
public class Ex1LocActivity extends ActionBarActivity {
TextView ex1_txt;
// 현재 위치값
double latitude, longitude; // 위도 경도
// 위치 정보 확인 및 관리를 위한 객체
LocationManager l_manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex1_loc);
ex1_txt = (TextView)findViewById(R.id.ex1_txt);
// GPS상태를 확인하는 함수
checkGPS();
}
private void checkGPS()
{
Builder a_dialog = new Builder(Ex1LocActivity.this);
a_dialog.setTitle("Location");
a_dialog.setMessage("use GPS??\n\n outside? \"YES\" \n inside? \"NO\" ");
a_dialog.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
init(true);
}
});
a_dialog.setNegativeButton("NO", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
init(false);
}
});
a_dialog.setCancelable(false); // 뒤로가기 버튼으로 취소 못하게 하기
a_dialog.show(); // 다이얼로그 보여주기
} // checkGPS()
private void init(boolean isGPS) {
String provider = null;
l_manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/*getSystemService()는 이제까지 imflate할 때와 Sensor사용할 때도 사용했었다. */
if (isGPS) {
// gps신호를 수신
provider = LocationManager.GPS_PROVIDER;
}
else
{
// wifi혹은 3g, 4g신호를 수신한다.
provider = LocationManager.NETWORK_PROVIDER;
}
Toast.makeText(getApplicationContext(), provider, 1000).show();
// locationUpdates(provider, 1초에 한번 , 최소거리 1m, 이벤트 감지자)
l_manager.requestLocationUpdates(provider, 1000, 1, l_listener);
} // init()
// 위치값이 변경되거나 현재 상태 또는
// 위치 기반을 사용하지 못하는 상태가 될 때를 감지하여
// 처리하는 이벤트 감지자
LocationListener l_listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// provider 상태가 변경되었을 때
}
@Override
public void onProviderEnabled(String provider) {
// gps를 사용할 수 있는 경우
Toast.makeText(getApplicationContext(), "find provider", 1000).show();
}
@Override
public void onProviderDisabled(String provider) {
// GPS가 꺼져있는 경우
Toast.makeText(getApplicationContext(), "no found provider", 1000).show();
}
@Override
public void onLocationChanged(Location location) {
// 현재 위치의 값이 변경될 때 수행되는 메소드
String str = null;
// Debug 메세지
Toast.makeText(getApplicationContext(), "onLocationChanged() called", 1000).show();
//위도와 경도
latitude = location.getLatitude();
longitude = location.getLongitude();
if (location != null)
{
str = "Latitude : " + latitude + "\nLongitude : " + longitude;
}
else
{
Toast.makeText(getApplicationContext(), "onLocationChanged() : location is null", 1000).show();
}
ex1_txt.setText(str);
}
};
protected void onDestroy() {
super.onDestroy();
if(l_manager != null)
l_manager.removeUpdates(l_listener);
};
}
단순히 현재 위치 좌표를 찍는 예제인데
왜인지 onLocationChanged() 함수가 호출이 안되네요.
왜일까요...