1

I'm sending a broadcast using

Intent intent = new Intent();
intent.setAction("com.example.android.name");
intent.putExtra("CODE", code);

in onReceive method of broadcast listener class when retrieving extra data using

String code = intent.getStringExtra("CODE");

I am getting null pointer exception. Any help how to retrieve the data.

public class ReceiveNetworkBroadcast extends BroadcastReceiver {

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

        Bundle extras = intent.getExtras();
        String code = extras.getString("CODE");
        Log.e("NET_BCAST_RECEIVER: ", code); 

    }

}

       <receiver
            android:name=".ReceiveNetworkBroadcast"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.android.name" />
            </intent-filter>
        </receiver>

public void sendBroadcast(Context context, String code){
        Intent intent = new Intent();
        intent.setAction("com.example.android.name");
        intent.putExtra("CODE", code);
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        context.sendBroadcast(intent);

    }
Akzwitch
  • 113
  • 1
  • 2
  • 13

1 Answers1

1

Try to get Intent extras in bundle like this :

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle extras = intent.getExtras();
     if (extras != null) {
      if(extras.containsKey("CODE")){
       Object value=extras.get("CODE");
       System.out.println(value);
      }
     }

}

Hope it will work now.

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65