>source

service 내부에서 위치 업데이트를 요청할 때이 오류가 발생합니다 . 위치 권한이 부여되었습니다. LocationManager  널이 아님, Provider  null이 아닙니다.

LocationManager: [LocationManager] requestLocationUpdates(), 
    java.lang.RuntimeException
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:1013)
        at android.location.LocationManager.requestLocationUpdates(LocationManager.java:595)
        at com.trackinglibrary.service.DataService.createAndRequestLocationUpdates(DataService.java:759)
        at com.trackinglibrary.service.DataService.onStartCommand(DataService.java:252)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3694)
        at android.app.ActivityThread.access$1600(ActivityThread.java:202)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:198)
        at android.app.ActivityThread.main(ActivityThread.java:6729)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

onStartCommand() 에서 전화하는 위치 코드 :

@SuppressLint("MissingPermission")
    private void createAndRequestLocationUpdates() {
        try {
            if (!TrackThatUtils.isLocationEnabled(getApplicationContext())) {
                Log.e(TAG, "Location is disable.");
                return;
            }
            if (locationManager == null) {
                locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                addProximityAlert();
            }
            if (locationManager == null || !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Log.e(TAG, "Location provider is disable.");
                return;
            }
            // getting GPS status
            boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            Log.e(TAG, "Location provider is: " + isGPSEnabled);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                Log.e(TAG, "== Error On onConnected() Permission not granted");
                //Permission not granted by user so cancel the further execution.
                return;
            }

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(true);
            //setCostAllowed(true): This flag enables LocationManager to exchange “data packet” with
            // 3G/4G network base stations in order to get better location
            criteria.setCostAllowed(true);
            criteria.setBearingRequired(false);
            //API level 9 and up
            criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
            criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
            String provider = locationManager.getBestProvider(criteria, true);
//          locationManager.requestLocationUpdates(gpsFreqInMillis, gpsFreqInDistance, criteria, locationListener, null);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, this);
            Log.i(TAG, "request for location updates..");
        } catch (SecurityException | IllegalArgumentException e) {
            Log.e(TAG, "Exception inside createAndRequestLocationUpdates(): " + e);
        }
    }


  • 답변 # 1

    공유 환경 설정에 위치 클래스를 저장하지 마십시오. 제 경우에는 이것이 문제였습니다.

  • 답변 # 2

    문서 RuntimeException 에 따르면  호출 스레드에 Looper가 없을 때 호출됩니다. Looper.prepare() 에 전화를 시도   LocationManager 에 전화하기 전에

  • 이전 android - Playstore에서 여러 버전의 애플리케이션을 유지 관리하려면 어떻게해야하나요?
  • 다음 mysql - 해시 된 비밀번호를 데이터베이스에 저장하는 중 오류