我在搞乱 Android SDK,并注意到在接收 SMS 消息的示例之一中,onReceive 的执行从未超过 Intent.getExtras()。可以肯定的是,我在该行之前和之后添加了日志,并且在执行 getExtras 之后没有日志。Android 模拟器版本为 4.0.3。谁能指出这段代码中我没有看到的一些缺陷,或者为这个看似随机且令人沮丧的问题提供解决方法或解决方案?
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.i("smsreceiver", "Intent: " + intent.getAction()); //Shows up in LogCat normally
Bundle bundle = intent.getExtras();
Log.i("smsreceiver", "after"); //this is never reached, and nothing after this is executed
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++)
{
smsMessage[n] = SmsMessage.createFromPdu((byte[])messages[n]);
}
Log.i("smsreceiver", "Message: " + smsMessage[0].getMessageBody());
}
}