Extra总是在registerreceiver中返回null

时间:2015-10-14 18:05:45

标签: android android-intent broadcastreceiver

在我的Android应用中,我希望能够在收到短信时通知活动,并向此活动提供发件人的号码。为了实现这个目标,我使用的是BroadcastReceiver类,它收集SMS发送者的电话号码并以广播的意图发送它们。

public class SMSReceiver extends BroadcastReceiver {
    final public static String SMS_BROADCAST = "myapp.BROADCAST_SMS";
    final public static String SMS_BROADCAST_EXTRA = "myapp.GET_SMS";

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle extras = intent.getExtras();
        if (extras == null)
            return;
        Object[] pdus = (Object[]) extras.get("pdus");
        SmsMessage Sms[] = new SmsMessage[pdus.length];
        String numbers[] = new String[pdus.length];
        //get the sms's
        for (int i = 0; i < pdus.length; i++) {
            Sms[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String sender = Sms[i].getOriginatingAddress();
            numbers[i] = sender;
        }
        //pass the number array to my activity via an intent.
        Intent intent2 = new Intent(SMS_BROADCAST);
        intent2.putExtra(SMS_BROADCAST_EXTRA, numbers);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent2);
    }
}

这是我在活动中注册接收器的地方。我收到了意图,但字符串“数字”数组始终为空。如果我尝试用它做某事,我总是有一个空指针异常。

 @Override
    public void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter(SMSReceiver.SMS_BROADCAST);
        BroadcastReceiver SmsReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(SMSReceiver.SMS_BROADCAST)) {
                    String numbers[] = getIntent().getStringArrayExtra(SMSReceiver.SMS_BROADCAST_EXTRA);
                    if (numbers == null) {
                        // This always appears in the log !!!
                        Log.d("NUMBERS", "IS NULL");
                        return;
                    }
                    // do something with the numbers
                }
            }
        };
        LocalBroadcastManager.getInstance(this).registerReceiver(SmsReceiver, intentFilter);
    }

我仍然无法看到我做错了什么,但我是Android开发中的新手。所以任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

您使用getIntent()意图使用方法onReceive代替getIntent()意图

更改为intentString numbers[] = intent.getStringArrayExtra(SMSReceiver.SMS_BROADCAST_EXTRA);

{
"gameid": "memoria",
"secao": "adver",
"nomeLocal": [
        {
            "language": "pt",
            "conteudo": "Descricao PT"
        },
        {
            "language": "en",
            "conteudo": "Descricao EN"
        }
    ],
"descricaoLocal": [
        {
            "language": "pt",
            "conteudo": "Jodo da Memória"
        },
        {
            "language": "en",
            "conteudo": "Peek Game"
        }
    ],
"features": [
        {
     "valor": 85,
     "conteudo": [
         {
             "language": "te",
             "conteudo": "teste"
         }
         ]
        }
    ]