Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetNetworkType in Android 11

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).

like image 544
tomerpacific Avatar asked Jul 02 '20 08:07

tomerpacific


1 Answers

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);
        }
    }
like image 55
EAS Stands with Ukraine Avatar answered Sep 19 '22 09:09

EAS Stands with Ukraine