LocationManager의 ProximityAlert(근접 알림) 기능을 사용하면 특정 위치에 접근 했을 때 처리할 동작을 지정할 수 있습니다.
[main.xml]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?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= "내 위치는 ? " /> <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/location" /> <Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/getLocation" android:text= "Where am I?" /> </LinearLayout> |
브로드캐스트 리시버 추가
지정된 위치에 접근하거나 벗어날 때 메시지를 표시하려면 브로드캐스트 리시버를 추가해야합니다. 매니페스트를 열어 Application 탭을 선택한 후, Application Nodes에서 Add... 버튼을 눌러 Receiver를 선택합니다.
다음, 속성 창에서 Name*을 눌러 브로드캐스트 리시버의 클래스를 추가합니다. Superclass로 android.content.BroadcastReceiver를 지정합니다.
브로드캐스트 코드 작성
브로드캐스트 리시버가 브로드캐스트 메시지를 받으면 onReceive() 메서드가 호출되며, 따라서 이 메서드에 메시지를 표시하는 코드를 추가합니다.
ProximityAlert를 통해 수신한 브로드캐스트 메시지를 포함합니다. 이 데이터는 LocationManager.KEY_PROXIMITY_ENTERING 키 값을 사용하여 받을 수 있으며, 이 값이 true라면 지정한 지점에 접근 중, flase면 벗어나고 있다는 의미입니다.
[LocationReceiver.java]
1 2 3 4 5 6 7 8 9 10 11 12 | public class LocationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { boolean isEntering = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false ); if (isEntering) Toast.makeText(context, "목표 지점에 접근중.." , Toast.LENGTH_LONG).show(); else Toast.makeText(context, "목표 지점에서 벗어납니다." , Toast.LENGTH_LONG).show(); } } |
[Locationinfo.java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | public class Locationinfo extends Activity { LocationManager locManager; LocationListener locationListener; LocationReceiver receiver; // 브로드캐스트 리시버의 인스턴스 정의 @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Button getLocation = (Button)findViewById(R.id.getLocation); final TextView locationText = (TextView)findViewById(R.id.location); locationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) { locationText.setText( "위도 : " + location.getLatitude() + " 경도 : " + location.getLongitude()); } @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 } }; getLocation.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000 , 0 , locationListener); } }); // 브로드캐스트 리시버가 메시지를 받을 수 있도록 설정 // 액션이 com.androidhuman.exmple.Location인 브로드캐스트 메시지를 받도록 설정 receiver = new LocationReceiver(); IntentFilter filter = new IntentFilter( "com.androidhuman.example.Location" ); registerReceiver(receiver, filter); // ProximityAlert 등록 Intent intent = new Intent( "com.androidhuman.example.Location" ); PendingIntent proximityIntent = PendingIntent.getBroadcast( this , 0 , intent, 0 ); locManager.addProximityAlert( 37.422006 , - 122.084095 , 100f, - 1 , proximityIntent); } // 브로드캐스트 메시지 수신 중단 public void onStop(){ super .onStop(); locManager.removeUpdates(locationListener); unregisterReceiver(receiver); } } |
브로드캐스트 메시지를 보내는 PendingIntent를 얻는 메서드
public static PendingIntent getBroadcast (Context context, int requestCode, Intent intent, int flags)
브로드캐스트 메시지를 보내는 PendingIntent의 인스턴스를 얻습니다.
ProximityAlert를 등록하는 메서드
public void addProximityAlert (double Latitude, double Longitude, float radius, long expiration, PendingIntent intent)
- latitude : 위도를 지정합니다.
- longitude : 경도를 지정합니다.
- radius : 지정한 위도와 경도를 중심으로 근접 알림을 제공하는 기준이 되는 변경을 지정합니다. (단위는 미터)
- expiration : 근접 알림이 발생한 후, 해당 지점에 대한 근접 알림을 해제하기까지의 지연 시간을 지정합니다. (단위는 밀리초) 근접 알림을 계속 제공하고 싶다면 -1을 입력합니다.
- intent : 근접 이벤트가 발생했을 때 실행할 PendingIntent를 지정합니다.
출처 - 기초부터 다지는 커니의 안드로이드
'프로그래밍 > Android' 카테고리의 다른 글
위치 정보 사용 - 현재 좌표 알기 (0) | 2011.08.25 |
---|---|
위치 기반 서비스 (1) | 2011.08.25 |
SharedPreferences 예제 (0) | 2011.08.24 |
SharedPreferences 설명 (0) | 2011.08.24 |
콘텐트 프로바이더 (0) | 2011.08.24 |