gpt4 book ai didi

Android getSimOperator() 因缺少权限而崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:46 25 4
gpt4 key购买 nike

运行以下代码时:

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

应用程序崩溃并显示以下日志:

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)

查看 Android 的源代码 getSimOperator() .我不明白代码如何在这种方法不需要的权限下崩溃?

调用 getDeviceId() 时我能够重现因为它需要 READ_PHONE_STATE。

我只在 android 5.1 版本上看到过这种情况。

最佳答案

使用这个方法效果很好

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

关于Android getSimOperator() 因缺少权限而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com