Caused by:java.lang.IllegalArgumentException: provider “network“ does not exist

Problem: The mobile phone location permission is turned on. When installing apk customized by some manufacturers, the first installation starts normally, but when it is started again, the application crashes. According to the system log, the problem is located in LocationManager.java. The system log is as follows:

Problem analysis: The app will apply for positioning permission, but in the system, the network provider does not exist. When the provider does not exist, calling requestLocationUpdates() of LocationManagerService.java, the Android system will throw an exception.

———————————analyze—————- ————————————————–

(1) When the app obtains the location, it will obtain the location through the api provided by LocationManager.java.

Source code path: frameworks/base/location/java/android/location/LocationManager.java

/**
      * @param provider providers listed by {@link #getAllProviders()}
      * @param locationRequest location request containing location parameters
      * @param executor The executor that handles listener callbacks
      * @param Listener The listener that receives location updates
      *
      * Throws IllegalArgumentException if the provider is null or does not exist
      * If locationRequest is null, throw IllegalArgumentException
      * If the listener is empty, @throws IllegalArgumentException
      * If there is no appropriate permission, @throwSecurityException is thrown
      */

 @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
    public void requestLocationUpdates(@NonNull String provider,
            @NonNull LocationRequest locationRequest,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull LocationListener listener) {
        Preconditions.checkArgument(provider != null, "invalid null provider");
        Preconditions.checkArgument(locationRequest != null, "invalid null location request");

        try {
            synchronized (sLocationListeners) {
                WeakReference<LocationListenerTransport> reference = sLocationListeners.get(
                        listener);
                LocationListenerTransport transport = reference != null ? reference.get() : null;
                if (transport == null) {
                    transport = new LocationListenerTransport(listener, executor);
                } else {
                    Preconditions.checkState(transport.isRegistered());
                    transport.setExecutor(executor);
                }

                //Method to call LocationManagerService
                mService.registerLocationListener(provider, locationRequest, transport,
                        mContext.getPackageName(), mContext.getAttributionTag(),
                        AppOpsManager.toReceiverId(listener));

                sLocationListeners.put(listener, new WeakReference<>(transport));
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

(2) Then call the registerLocationListenner() method of LocationManagerService.

Source code path: frameworks/base/services/core/java/com/android/server/location/LocationManagerService.java

 @Override
    public void registerLocationListener(String provider, LocationRequest request,
            ILocationListener listener, String packageName, @Nullable String attributionTag,
            String listenerId) {
        CallerIdentity identity = CallerIdentity.fromBinder(mContext, packageName, attributionTag,
                listenerId);
        int permissionLevel = LocationPermissions.getPermissionLevel(mContext, identity.getUid(),
                identity.getPid());
        LocationPermissions.enforceLocationPermission(identity.getUid(), permissionLevel,
                PERMISSION_COARSE);

        // clients in the system process should have an attribution tag set
        if (identity.getPid() == Process.myPid() & amp; & amp; attributionTag == null) {
            Log.w(TAG, "system location request with no attribution tag",
                    new IllegalArgumentException());
        }

        request = validateLocationRequest(provider, request, identity);

        LocationProviderManager manager = getLocationProviderManager(provider);
        //When the provider does not exist, throw an exception
        Preconditions.checkArgument(manager != null,
                "provider "" + provider + "" does not exist");

        manager.registerLocationRequest(request, identity, permissionLevel, listener);
    }

(3) As can be seen from the above source code, when the LocationProvider is null, the system will throw an exception. This is the exception seen in the log. Therefore, the following steps can be performed only when the locationProvider is not null. LocationManager.java provides an isProviderEnabled() method to determine whether the provider is available.

Source code path: frameworks/base/location/java/android/location/LocationManager.java

//If the provider exists and is enabled, returns true; if provider is null, throws IllegalArgumentException

 public boolean isProviderEnabled(@NonNull String provider) {
        return isProviderEnabledForUser(provider, Process.myUserHandle());
    }

(4) Therefore, the final solution is:

Source code path: frameworks/base/location/java/android/location/LocationManager.java

/**
      * @param provider providers listed by {@link #getAllProviders()}
      * @param locationRequest location request containing location parameters
      * @param executor The executor that handles listener callbacks
      * @param Listener The listener that receives location updates
      *
      * Throws IllegalArgumentException if the provider is null or does not exist
      * If locationRequest is null, throw IllegalArgumentException
      * If the listener is empty, @throws IllegalArgumentException
      * If there is no appropriate permission, @throwSecurityException is thrown
      */

 @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION})
    public void requestLocationUpdates(@NonNull String provider,
            @NonNull LocationRequest locationRequest,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull LocationListener listener) {
        Preconditions.checkArgument(provider != null, "invalid null provider");
        Preconditions.checkArgument(locationRequest != null, "invalid null location request");

        try {
            synchronized (sLocationListeners) {
                WeakReference<LocationListenerTransport> reference = sLocationListeners.get(
                        listener);
                LocationListenerTransport transport = reference != null ? reference.get() : null;
                if (transport == null) {
                    transport = new LocationListenerTransport(listener, executor);
                } else {
                    Preconditions.checkState(transport.isRegistered());
                    transport.setExecutor(executor);
                }

      //When the provider named NETWORK_PROVIDER is unavailable, the positioning information will not be updated.
      
    + if(!isProviderEnabled(NETWORK_PROVIDER))
    + return ;

                //Method to call LocationManagerService
                mService.registerLocationListener(provider, locationRequest, transport,
                        mContext.getPackageName(), mContext.getAttributionTag(),
                        AppOpsManager.toReceiverId(listener));

                sLocationListeners.put(listener, new WeakReference<>(transport));
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 133445 people are learning the system