Search code examples
androidandroid-intentbroadcastreceiver

Android getExtra always returning null


I am creating an intent and adding an extra parameter to it:

public void stop(View v) {
    AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = am.getStreamVolume(AudioManager.STREAM_RING);
    Intent intent = new Intent();
    intent.setAction(CANCEL_ACTION);
    intent.putExtra("volume", currentVolume);
    sendBroadcast(intent);
}

I get the intent in my BroadcastReceiver, but the "volume" is always null:

public void onReceive(Context context, Intent intent) {
    if (intent != null) {
        if (intent.getAction().equals("com.hamm.ringeroff.cancel")) {
            String _volume = intent.getStringExtra("volume");
        }
    }
}

What am I missing? It has to be something simple. Thanks.


Solution

  • Either currentVolume is String and null or it is int in which case, use getIntExtra