안드로이드 개발 질문/답변
(글 수 45,052)
List<String> providers =locationManager.getProviders(true);
여기서 list가 비어 있다고 나오고 for문 안으로 들어가지를 못하네요..
왜 그런지 알 수가 없어서...ㅠ
아래가 코드이구요.
package com.example.locationtest;
import java.util.List;
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.widget.TextView;
public class LocationTest extends Activity {
/** Called when the activity is first created. */
LocationManager locationManager;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
String location_context = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager)getSystemService(location_context);
TextView tv = (TextView)findViewById(R.id.TextView01);
StringBuilder sb = new StringBuilder("이용 가능한 공급자: ");
List<String> providers =locationManager.getProviders(true);
for(String provider : providers){
locationManager.requestLocationUpdates(provider, 1000, 0, new LocationListener(){
public void onLocationChanged(Location location){}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
});
sb.append("\n").append(providers).append(":");
Location location = locationManager.getLastKnownLocation(provider);
if(location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
sb.append(lat).append(", ").append(lng);
}
else{
sb.append("위치 없음");
}
}
tv.setText(sb);
//setContentView(R.layout.main);
}
}
Manifest 이구요..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationtest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationTest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
<user-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS" />
</manifest>
main.xml 입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView android:text="TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView01"></TextView>
</LinearLayout>
2009.08.29 15:49:04
LocationManager로 provider를 가져오는 방법은 몇가지(getAllProvider,getBestProvider,getProvider,getProviders)있는데요
enable된 Provider "getProviders(true")로는 null 값이 되는 것 맞네요.
getProvider(false)로 하면 "gps"와 "network"를 가져옴니다.
어쩌거나 어느용도로 사용하는 지는 모르나
enable된 여러개의 provider를 가져오는 것은 어려운것 같습니다. (emulator 에서는)
보통은 적절한 provider를 가져오는 방법은 criteria를 setting하고
LocationManager에서 getBestProvider를 이용하여 최적의 provider를 가져오죠.
enable된 Provider "getProviders(true")로는 null 값이 되는 것 맞네요.
getProvider(false)로 하면 "gps"와 "network"를 가져옴니다.
어쩌거나 어느용도로 사용하는 지는 모르나
enable된 여러개의 provider를 가져오는 것은 어려운것 같습니다. (emulator 에서는)
보통은 적절한 provider를 가져오는 방법은 criteria를 setting하고
LocationManager에서 getBestProvider를 이용하여 최적의 provider를 가져오죠.
혹시 GPS 가 Disable 된것 아님니까?
Emulator에서 확인 방법은 (Locale이 한국 인 경우)
설정 -> 보안 및 위치 -> GPS 위성 사용
저도 이것땜시 몇시간 헤멨다는...