public class Lo extends Activity implements LocationListener{
private LocationManager locManager;
Geocoder geoCoder;
Location myLocation = null;
double latPoint = 0;
double lngPoint = 0;
SQLiteDatabase sqldb;
DBHelper db;
String b;
ImageButton gpsButton,sendButton;
String i,j,k;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lo);
db = new DBHelper(getApplicationContext());
// TODO Auto-generated method stub
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// LocationListener의 핸들을 얻음
// GPS로 부터 위치정보 받음
// locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
// 5, this);
// 네트워크로 위치정보를 받음
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
1000, 5, this);
// 주소를 확인함
geoCoder = new Geocoder(this, Locale.KOREAN);
gpsButton = (ImageButton)findViewById(R.id.gpsButton);
sendButton = (ImageButton)findViewById(R.id.sendButton);
gpsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
GetLocations();
}
});
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
GetLocations();
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sqldb = db.getWritableDatabase();
Cursor cursor = sqldb.rawQuery("SELECT * FROM CallDB;",null);
while(cursor.moveToNext()){
b = cursor.getString(0);
}
String a="sms:ok";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("0"+b, null, "위도:"+k+"경도:"+j+"주소:"+i, null, null);
Toast.makeText(getApplicationContext(),a , Toast.LENGTH_SHORT).show();
cursor.close();
sqldb.close();
}
});
}
public void GetLocations() {
TextView latText = (TextView) findViewById(R.id.tvLatitude);
TextView lngText = (TextView) findViewById(R.id.tvLongitude);
TextView jusoText = (TextView) findViewById(R.id.tvAddress);
StringBuffer juso = new StringBuffer();
if (myLocation != null) {
latPoint = myLocation.getLatitude();
lngPoint = myLocation.getLongitude();
try {
// 위도,경도를 이용하여 현재 위치의 주소를 가져온다.
List<Address> addresses;
addresses = geoCoder.getFromLocation(latPoint, lngPoint, 1);
for (Address addr : addresses) {
int index = addr.getMaxAddressLineIndex();
for (int i = 0; i <= index; i++) {
juso.append(addr.getAddressLine(i));
juso.append(" ");
}
juso.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
latText.setText(String.valueOf(latPoint));
lngText.setText(String.valueOf(lngPoint));
jusoText.setText(String.valueOf(juso));
k = String.valueOf(latPoint);
j = String.valueOf(lngPoint);
i = String.valueOf(juso);
}
public void onLocationChanged(Location location) {
Log.d("location", "location changed");
myLocation = location;
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
public void onStatusChanged(String s, int i, Bundle bundle) {
}
}
질문이있습니다. 이코드에서
gpsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
GetLocations();
}
});
이부분이 잘실행됍니다 버튼누르면 GetLocations()에서 수행하는 현재위치를 setText해서 가져옵니다.
근데 저거를 버튼 클릭리스너없이 그냥 GetLocations();만 쓰면 왜 수행을 못할가요
버튼클릭없이 자동으로 실행되게 하고싶은데...