代码之家  ›  专栏  ›  技术社区  ›  Rahul

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

  •  0
  • Rahul  · 技术社区  · 5 年前

    请求内部位置更新时出现此错误 service LocationManager 不为空, Provider 不为空。

    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);
            }
        }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Paweł Dedio    5 年前

    根据 documentation RuntimeException 当调用线程没有Looper时调用。试着打电话 Looper.prepare() 在你打电话之前 LocationManager

        2
  •  -1
  •   Rahul    5 年前

    不要储存 Location