소스는 밑에 있는 내용이구요~
제가 지정해 놓은 위도 경도가 있고 radius를 1000으로 1km반경을 목표로 합니다!!!
현재 제위치가 1km반경 내에 있을때는 리시버로 안전 위치에 있음을 다이어로그로 알려주고~
현재위치를 다시 받아왔을때 1km반경을 벗어난 위치에 있다면 리시버에서 안전위치를 벗어났다고
다이어로그창으로 알리고 싶습니다!!! 밑에 내용데로 구현했는데~ 잘되지 않네요~
어떤게 틀렸는지~ 아시는분 좀 알려주세요~
private void setupProximityAlert() {
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d(TAG2, "Registering ProximityAlert");
Intent intent = new Intent(POI_REACHED);
proximityIntent =
PendingIntent.getBroadcast(getApplicationContext(), 0, intent,
PendingIntent.FLAG_ONE_SHOT);
locationManager.addProximityAlert(latitude,
longitude, radius, 1000000,
proximityIntent);
IntentFilter intentFilter = new IntentFilter(POI_REACHED);
registerReceiver(new ProximityAlertReceiver(),intentFilter);
}
else {
Log.d(TAG2, "GPS_PROVIDER not available");
}
}
private class ProximityAlertReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
// Toast.makeText(Safety.this, "Treasure: " + entering,Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(Safety.this);
if(entering == true)
{
new AlertDialog.Builder(Safety.this)
.setTitle("알립니다")
.setMessage("안전 위치를 벗어났습니다")
.setIcon(R.drawable.icon)
.setPositiveButton("닫기", null)
.show();
}
else
{
new AlertDialog.Builder(Safety.this)
.setTitle("알립니다")
.setMessage("안전 위치입니다")
.setIcon(R.drawable.icon)
.setPositiveButton("닫기", null)
.show();
}
전체소스를 안봐서 모르겠지만 현재 setupProximityAlert() 는 딱 한번만 GPS 정보의 변화를 받습니다.
GPS정보 변하는걸 한번 방송하고 디엔드. 그 한번이 아무 관계없는 위치라도 방송한번 하고 끝.
소스를 복사해다가 해봤는데 잘 작동합니다. 상황을 맞춘 딱 한번뿐이지만...