package com.example.locationdemoa2;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
LocationManager locMgr;
LocationListener locLnr;
TextView text;
Button startButton;
Button stopButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locLnr = new MyLocationListener();
text = (TextView) findViewById(R.id.text);
startButton = (Button) findViewById(R.id.start);
stopButton = (Button) findViewById(R.id.stop);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int millis = 0;
int distance = 0;
if(v == startButton) {
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, millis, distance, locLnr);
text.setText("위치 감지를 시작합니다.");
} else if(v == stopButton) {
locMgr.removeUpdates(locLnr);
text.setText("위치 감지를 중지합니다.");
}
}
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
String text = "현재위치 : \n" + "위도 : " + loc.getLatitude() + "\n" + "경도 : " + loc.getLongitude();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS 이용 불가", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "GPS 이용 가능", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
교재의 내용을 그대로 코딩했음에도 불구하고 gps위치정보를 받아오지 못하고 있습니다.
아예 onLocationChanged함수가 호출이 안되는데요 그 이유가 뭘까요?
위의 소스코드는 제가 작성한 소스코드입니다.