为什么在调试时,intent.getExtras()。mMap为null?

时间:2015-04-02 07:58:39

标签: android android-intent android-bundle

我有这样的BroadCastReceiver:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println(Arrays.toString(intent.getExtras().keySet().toArray()));

    }
}

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.angryyogurt.yotransfer">

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <receiver android:name=".NewIncomingReceiver">
        <intent-filter>
            <action
                android:name="android.provider.Telephony.SMS_RECEIVED"
                android:priority="1010"/>
            <action
                android:name="android.intent.action.NEW_OUTGOING_CALL"
                android:priority="1010"/>
            <action
                android:name="android.intent.action.PHONE_STATE"
                android:priority="1010"/>
        </intent-filter>
    </receiver>

</application>

</manifest>

我用它来收听短信。当我尝试向此手机发送消息时,它会打印密钥集[format, pdus, slot, phone, subscription]

但是当我调试它并在System.out.println...设置断点时,我看到intent.mExtras.mMap为空。为什么是这样?如何在调试期间检查意图中的地图项目?

1 个答案:

答案 0 :(得分:3)

实例变量mMap仅在附加组件被解组(未组合,反序列化)后填充。如果您没有做任何事情导致额外内容被取消编组,mMap将为null

试试这个:

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean b = intent.hasExtra("blah");
        System.out.println(Arrays.toString(
                intent.getExtras().keySet().toArray()));
    }
}

调用hasExtra()会强制将附加内容Bundle取消编组。在System.out.println()上设置您的断点,您应该会在mMap中看到其他内容。