最近,我收到一部手机的崩溃报告:LG-P970。我正在做的是在收到短信时读取短信。没什么不寻常的。我的代码在其他所有手机上运行。
Android – Listen For Incoming SMS Messages
与链接的答案一样,我的代码几乎相同
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
if (!Build.MODEL.equals("LG-P970")) {
final Bundle bundle = intent.getExtras();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
String message = "";
String senderNum = "";
SmsMessage currentMessage;
for (int i = 0; i < pdusObj.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
} else {
currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
senderNum = currentMessage.getDisplayOriginatingAddress();
message = message + currentMessage.getDisplayMessageBody();
}
//some logic
...
}
}
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
但是有一部手机在这条线路上坠毁SmsMessage.createFromPdu((byte[]) pdusObj[i]);
当代码进入&#39;试图捕获&#39;时发生了崩溃。块。
这是我的错误日志
Fatal Exception: java.lang.ExceptionInInitializerError
at com.a.b.c.SmsReceiver.void onReceive(android.content.Context,android.content.Intent)(SourceFile:38)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2139)
at android.app.ActivityThread.access$1500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4516)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10015 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1327)
at android.os.Parcel.readException(Parcel.java:1281)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getSubscriberId(IPhoneSubInfo.java:223)
at android.telephony.TelephonyManager.getSubscriberId(TelephonyManager.java:720)
at com.android.internal.telephony.lgeAutoProfiling.getInstanceSimInfo(lgeAutoProfiling.java:449)
at com.android.internal.telephony.lgeAutoProfiling.StartProfiling(lgeAutoProfiling.java:542)
at com.android.internal.telephony.lgeAutoProfiling.getValue(lgeAutoProfiling.java:495)
at com.android.internal.telephony.lgeAutoProfiling.getInteger(lgeAutoProfiling.java:325)
at android.telephony.SmsMessage.(SmsMessage.java:218)
at com.a.b.c.SmsReceiver.void onReceive(android.content.Context,android.content.Intent)(SourceFile:38)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2139)
at android.app.ActivityThread.access$1500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4516)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(NativeStart.java)
是的,我已加入清单。
<receiver android:name=".c.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
是的,我获得了权限。
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
所以我的问题是有什么解决方案吗?我不想只为一部手机添加其他权限。
我目前无视此手机阅读短信状态。
if (!Build.MODEL.equals("LG-P970")) { ... }