6
votes

When running the following code:

try {
    TelephonyManager telephonyManager = (TelephonyManager) MainApplication.getAppContext()
                                                                          .getSystemService(Context.TELEPHONY_SERVICE);
    String simMCCMNC = telephonyManager.getSimOperator();
} catch (Exception e) {
    Crashlytics.logException(e);
}

The app crashes with the following log:

Non-fatal Exception: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10076 nor current process has android.permission.READ_PHONE_STATE. at android.os.Parcel.readException(Parcel.java:1546) at android.os.Parcel.readException(Parcel.java:1499) at com.android.internal.telephony.ISub$Stub$Proxy.getActiveSubscriptionInfoForSimSlotIndex(ISub.java:459) at android.telephony.SubscriptionManager.getDefaultSmsSubId(SubscriptionManager.java:910) at android.telephony.TelephonyManager.getSimOperatorNumeric(TelephonyManager.java:1622) at android.telephony.TelephonyManager.getSimOperator(TelephonyManager.java:1592) at com.myapp.app.ApplicationInitializer.generateBaseUrl(SourceFile:297) at com.myapp.app.ApplicationInitializer.initFailProofComponents(SourceFile:193) at com.myapp.app.ApplicationInitializer.prepareManager(SourceFile:121) at com.myapp.managers.base.BaseManager.prepareManagerIfPossible(SourceFile:43) at com.my.app.ApplicationInitializer.init(SourceFile:137) at com.my.app.MainApplication.onCreate(SourceFile:23) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4628) at android.app.ActivityThread.access$1500(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)

Looking at the source code for Android getSimOperator(). I do not understand how the code can crash on a permission which is not required for this method?

I was able to reproduce when calling getDeviceId() because it requires READ_PHONE_STATE.

I've only seen this happen on android version 5.1.

2
can it be that all clients throwing that exception are rooted? - Tal Kanel

2 Answers

-2
votes

Use this method its work fine

DeviceInfo deviceInfo = new DeviceInfo();
        TelephonyManager tMgr = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
        deviceInfo.setDeviceId(Util.getDeviceID(mContext));
        if(tMgr!=null)
        {
            String simSerialNumber=tMgr.getSimSerialNumber();
            if(simSerialNumber!=null) {
                deviceInfo.setSimOperator(tMgr.getSimOperator());
                deviceInfo.setSimCountryIso(tMgr.getSimCountryIso());

                deviceInfo.setSimSerialNumber(simSerialNumber);
                deviceInfo.setSimOperatorName(tMgr.getSimOperatorName());
                deviceInfo.setSimNetworkOperator(tMgr.getNetworkOperator());
            }
        }
-2
votes

Use below method

 TelephonyManager manager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    String carrierName = manager.getNetworkOperatorName();

    Toast.makeText(MainActivity.this, ""+carrierName, Toast.LENGTH_SHORT).show();

You can retrive selected sim operator name...