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

Problem: The mobile phone location permission is turned on. When installing a customized apk, 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.

(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

 @UnsupportedAppUsage
    private void requestLocationUpdates(LocationRequest request, LocationListener listener,
            Looper looper, PendingIntent intent) {

        String packageName = mContext.getPackageName();

        // wrap the listener class
        ListenerTransport transport = wrapListener(listener, looper);

        try {

            //Call the method in LocatonManagerService
            mService.requestLocationUpdates(request, transport, intent, packageName);

       } catch (RemoteException e) {
           throw e.rethrowFromSystemServer();
       }
    }

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

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

 @Override
 public void requestLocationUpdates(LocationRequest request, ILocationListener listener,
            PendingIntent intent, String packageName) {


                      ...

  Receiver receiver;
                if (intent != null) {
                    receiver = getReceiverLocked(intent, pid, uid, packageName, workSource,
                            hideFromAppOps);
                } else {
                    receiver = getReceiverLocked(listener, pid, uid, packageName, workSource,
                            hideFromAppOps);
                }
 
                //further calls
                requestLocationUpdatesLocked(sanitizedRequest, receiver, uid, packageName);
            
} finally {
                Binder.restoreCallingIdentity(identity);
            }

                     ......

(3) Then call requestLocationUpdatesLocked()

 @GuardedBy("mLock")
    private void requestLocationUpdatesLocked(LocationRequest request, Receiver receiver,
            int uid, String packageName) {
        // Figure out the provider. Either its explicit request (legacy use cases), or
        // use the fused provider
        if (request == null) request = DEFAULT_LOCATION_REQUEST;
        //Get the required provider
        String name = request.getProvider();
        //provider name cannot be empty, otherwise an exception will be thrown
        if (name == null) {
            throw new IllegalArgumentException("provider name must not be null");
        }
       
        //Get locationProvider through provider name
        LocationProvider provider = getLocationProviderLocked(name);

        // When locationProvider is null, an exception will be thrown
        if (provider == null) {
            throw new IllegalArgumentException("provider doesn't exist: " + name);
        }

        UpdateRecord record = new UpdateRecord(name, request, receiver);
        if (D) {
            Log.d(TAG, "request " + Integer.toHexString(System.identityHashCode(receiver))
                     + " " + name + " " + request + " from " + packageName + "(" + uid + " "
                     + (record.mIsForegroundUid ? "foreground" : "background")
                     + (isThrottlingExemptLocked(receiver.mCallerIdentity)
                    ? "[whitelisted]" : "") + ")");
        }

        UpdateRecord oldRecord = receiver.mUpdateRecords.put(name, record);
        if (oldRecord != null) {
            oldRecord.disposeLocked(false);
        }

        if (!provider.isUseableLocked() & amp; & amp; !isSettingsExemptLocked(record)) {
            // Notify the listener that updates are currently disabled - but only if the request
            // does not ignore location settings
            receiver.callProviderEnabledLocked(name, false);
        }

        applyRequirementsLocked(name);

        // Update the monitoring here just in case multiple location requests were added to the
        // same receiver (this request may be high power and the initial might not have been).
        receiver.updateMonitoring(true);
    }

(4) 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());
    }

(5) Therefore, the final solution is:

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

 @UnsupportedAppUsage
    private void requestLocationUpdates(LocationRequest request, LocationListener listener,
            Looper looper, PendingIntent intent) {

        String packageName = mContext.getPackageName();

        // wrap the listener class
        ListenerTransport transport = wrapListener(listener, looper);

        try {

           //If the LocationProvider named NETWORK_PROVIDER is null, the subsequent process of obtaining the location will not continue.
  + if (!isProviderEnabled(NETWORK_PROVIDER))
  + return;

            //Call the method in LocatonManagerService
            mService.requestLocationUpdates(request, transport, intent, packageName);

       } 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 133895 people are learning the system