마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

gps nullporint 질문입니다.

+1 추천
String context = Context.LOCATION_SERVICE;
  
  locationManager = (LocationManager) getSystemService(context);
  Criteria criteria = new Criteria();
  criteria.setAccuracy(Criteria.ACCURACY_FINE); // 정확도
  criteria.setPowerRequirement(Criteria.POWER_LOW); // 전원 소비량
  criteria.setAltitudeRequired(false); // 고도, 높이 값을 얻어 올지를 결정
  criteria.setBearingRequired(false); // provider 기본 정보
  criteria.setSpeedRequired(false); // 속도
  criteria.setCostAllowed(true);

  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
  {
   String provider = locationManager.getBestProvider(criteria, true);
   lastKnownLocation = locationManager.getLastKnownLocation(provider);
   if(lastKnownLocation == null)
   {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    lastKnownLocation = locationManager.getLastKnownLocation(provider);
   }
   if(lastKnownLocation == null)
   {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.gps_fail).setCancelable(false)
      .setPositiveButton(R.string.gps_yes, new DialogInterface.OnClickListener()
      {
       public void onClick(DialogInterface dialog, int id)
       {
        dialog.cancel();
       }
      });
    AlertDialog alert = builder.create();
    alert.show();
   }
   else
   {
    Latitude = lastKnownLocation.getLatitude();
    Longitude = lastKnownLocation.getLongitude();
   }
//   lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  } 
   else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
  {
    String provider = locationManager.getBestProvider(criteria, true);
    lastKnownLocation = locationManager.getLastKnownLocation(provider);
    if(lastKnownLocation == null)
    {
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
     lastKnownLocation = locationManager.getLastKnownLocation(provider);
    }
    if(lastKnownLocation == null)
    {
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage(R.string.gps_fail).setCancelable(false)
       .setPositiveButton(R.string.gps_yes, new DialogInterface.OnClickListener()
       {
        public void onClick(DialogInterface dialog, int id)
        {
         dialog.cancel();
        }
       });
     AlertDialog alert = builder.create();
     alert.show();
    }
    else
    {
     Latitude = lastKnownLocation.getLatitude();
     Longitude = lastKnownLocation.getLongitude();
    }
  }

이부분에서 네트워크쪽에서는 null값이 들어오지 않지만

gps부분에서는 공기계 gps를 잡았을경우에만 provider에 null 값이 들어옵니다 그래서 lastknownLocation 에서 null값이 들어와 밑에 if 문에서 null일 경우 업데이트를 시켜서 위도 경도 값을 가져오려고 하였는데 결국에는 null 들어와 다이얼로그가 실행되더군요 ㅠㅠ gps부분에서만 null값이고 와이파이나 3g/4g 일경우에는 정상적으로 실행이 되는데

어디가 잘못된건지 감을 못잡겠네요 ㅠㅠ 도와주세요 ㅠ 조언좀부탁드릴꼐요 ㅠ

스릉스릉 (1,260 포인트) 님이 2014년 2월 5일 질문
요약하면 '네트웍에 연결되지 않으면 GPS 정보가 오지 않는다' 인가요?
쎄미님 답변감사합니다.
네트웍일때만 위도 경도가 넘어오고 gps일 경우 null값이 넘어옵니다.ㅠ 권한은 다 준상태입니다.
locationListener를 상속 받으시면 onlocationchanged, onStatusChanged라는게 있을거예요, 거기서 gps로 들어오면 저 소스를 실행하게 하시면 될것 같은데요
gps가 바로 뙇하고 잡히는게 아닙니당

1개의 답변

0 추천
설정 - 위치 - gps사용에 체크 돼있는지 확인해보세요
쎄미 (162,410 포인트) 님이 2014년 2월 5일 답변
...