简体   繁体   中英

Android [LocationManager] requestLocationUpdates(), java.lang.RuntimeException

I am getting this error when requestion for location updates inside service . Location permission is granted, LocationManager is not null, Provider is not 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)

Location code that I am calling inside 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);
        }
    }

不要将Location类存储在共享首选项中就我而言,这就是问题所在

According to the documentation RuntimeException is called when calling thread has no Looper. Try to call Looper.prepare() before you call LocationManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM