안녕하세요.
추운날씨에 감기 조심하십시요. ^-^)
아래와 같이 내 위치 정보를 가져오는 Code을 작성했는데 실제 동작시 LogCat에 어떠한 로그 내용도 기록되지 않습니다.
Log.d("MyTag", "MyLocation Invoked");의 내용조차 실행되지 않습니다.
따라서 결과값을 확인 할 수가 없는데, 제 코드상에 문제가 있는 걸까요?
알려주시면 감사하겠습니다. ^^)
[Code1]
Button stopService;
...
stopService = (Button)findViewById(R.id.btn_test2);
...
stopService.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// 위치정보를 호출합니다.
MyLocation location = new MyLocation();
}
});
...
[Code2]
package com.android.besafe;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MyLocation extends Activity implements LocationListener{
String provider;
LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d("MyTag", "MyLocation Invoked");
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
Location location = locationManager.getLastKnownLocation(bestProvider());
locationManager.requestLocationUpdates(bestProvider(),1000,0, this);
updateLocation(location);
Log.d("MyTag", "context: "+context);
}
public String bestProvider(){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
Log.d("MyTag", "bestProvider() Invoked");
return provider;
}
public void updateLocation(Location location) {
Log.d("MyTag","location: "+location);
if(location!=null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
List<Address> addresses = null;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try{
addresses = gc.getFromLocation(latitude, longitude, 1);
String now_address = addresses.get(0).getAddressLine(0);
Log.d("MyTag","현재위치: "+now_address);
}catch(IOException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
MyLocation으로 화면 출력을 하진 않을거라서 Activity을 상속하진 않을거지만, 일단 엡뽀님 말씀처럼 해봤습니다.
결론적으로 실행이 되지만 제가 중간에 Logging을 걸어둔 부분의 값들이 전부 NULL로 return 됩니다.
---------------------------------------------------------------------------------------------
12-15 01:40:55.541: D/MyTag(522): MyLocation Invoked
12-15 01:40:55.581: D/MyTag(522): bestProvider() Invoked
12-15 01:40:55.581: D/MyTag(522): bestProvider() Invoked
12-15 01:40:55.611: D/MyTag(522): location: null
12-15 01:40:55.611: D/MyTag(522): context: location




MyLocation location = new MyLocation(); 를
Intent intent = new Intent(Code1.this, MyLocation .class);
startActivity(intent);로 해보세요.
p.s) Activity 클래스는 객체 생성만 한다고 해서 실행된다거나 하지않습니다.