1

I have a BroadCastReceiver like this:

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>

I use this to listen to the SMS. When I try to send message to this phone, it prints the key set [format, pdus, slot, phone, subscription].

But when I debug it and set the breakpoint at System.out.println..., I see that intent.mExtras.mMap is null. Why is this? How to check items of the map in intent during debugging?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
AngryYogurt
  • 755
  • 2
  • 7
  • 22

1 Answers1

4

The instance variable mMap is only populated after the extras are unmarshalled (unparceled, deserialized). If you haven't done anything to cause the extras to be unmarshalled, mMap will be null.

Try this:

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()));
    }
}

calling hasExtra() will force the extras Bundle to be unmarshalled. Set your breakpoint on the System.out.println() and you should see the extras in mMap.

David Wasser
  • 93,459
  • 16
  • 209
  • 274