0

最近我收到了一部手机的崩溃报告:LG-P970。我正在做的是在收到短信时阅读短信。没有什么不寻常的。我的代码适用于其他所有手机。

Android – 监听传入的 SMS 消息

就像在链接的答案中一样,我有几乎相同的代码

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]);崩溃了崩溃发生在“try-catch”块中的代码时。

这是我的错误日志

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")) { ... }
4

0 回答 0