13

Following the changes posted here, the getNetworkType method is deprecated from Android R and onwards.

When trying to use this method in a R compiled application, results in the following exception being thrown:

java.lang.SecurityException: getDataNetworkTypeForSubscriber: uid 10225 does not have android.permission.READ_PHONE_STATE.
  at android.os.Parcel.createExceptionOrNull(Parcel.java:2285)
  at android.os.Parcel.createException(Parcel.java:2269)
  at android.os.Parcel.readException(Parcel.java:2252)
  at android.os.Parcel.readException(Parcel.java:2194)
  at com.android.internal.telephony.ITelephony$Stub$Proxy.getNetworkTypeForSubscriber(ITelephony.java:7565)
  at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:2964)
  at android.telephony.TelephonyManager.getNetworkType(TelephonyManager.java:2928)
  at com.ironsource.environment.ConnectivityService.getCellularNetworkType(ConnectivityService.java:197)
  at com.ironsource.sdk.service.DeviceData.updateWithConnectionInfo(DeviceData.java:98)
  at com.ironsource.sdk.service.DeviceData.fetchMutableData(DeviceData.java:54)
  at com.ironsource.sdk.service.TokenService.collectDataFromDevice(TokenService.java:120)
  at com.ironsource.sdk.service.TokenService.getRawToken(TokenService.java:177)
  at com.ironsource.sdk.service.TokenService.getToken(TokenService.java:166)
  at com.ironsource.sdk.IronSourceNetwork.getToken(IronSourceNetwork.java:183)

This is fine and is expected according to the documentation. If I compile the application to any version before Android R, the exception doesn't show.

This exception indicates that I need to request the android.permission.READ_PHONE_STATE permission.

I wanted to know if there is a way to get the network type with any other API that does NOT require this permission (as this permission's level is dangerous and I would rather not ask the user for it).

tomerpacific
  • 3,092
  • 8
  • 22
  • 41

4 Answers4

3

So my friend I have the same trouble as you but so far I came with a temporary fix I use the compileSdkVersion 29 not 30 as well as the targetSdkVersion 29 and my buildToolsVersion 28.0.3 and my app is loading fine.

Since this problem by me its coming due a third party library so till the fix the error I can not fix it alone, but I think with this temporary solution for now is quite well.

Vuki Limani
  • 106
  • 1
  • 5
1

You can still use getDataNetworkType(); This method does not necessarily need READ_PHONE_STATE, as stated in his Doc, but that it's sufficient "that the calling app has carrier privileges".

https://developer.android.com/reference/android/telephony/TelephonyManager#getDataNetworkType()

For what I know about getting those privigileges, it could be tricky/really hard, you may look into getting carrier privileges and using this method, which is also the suggested substitution for getNetworkType().

CSR
  • 31
  • 1
  • 4
  • I have obviously read this, but having the application have carrier privileges is not something that is easy to come by (like you stated). So, this isn't really that helpful. .. – tomerpacific Jul 12 '20 at 07:59
1

This method necessarily need READ_PHONE_STATE by this way in your activity not just manifest >>>

// Check if the READ_PHONE_STATE permission is already available.


if(ActivityCompat.checkSelfPermission(this,Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {

            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_PHONE_STATE)) {

                  //here >> use getNetworkType() method
              // like this example
 
              mStationInfo.set_networkType(mTelephonyManager.getNetworkType());
            } 
           else {}
ewong
  • 1,072
  • 9
  • 20
1

Take runtime permission for READ_PHONE_STATE to ignore crash of getDataNetworkTypeForSubscriber

 @Override
    protected void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {

            int res = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
            if (res != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{android.Manifest.permission.READ_PHONE_STATE}, 123);
            }

        }
    }

    private final static int REQUEST_CODE_ASK_PERMISSIONS = 1002;

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_ASK_PERMISSIONS:
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "READ_PHONE_STATE Denied", Toast.LENGTH_SHORT)
                            .show();
                } else {
                }
                stepAfterSplash();

                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
EAS
  • 11
  • 4