VOLUME_CHANGED_ACTION get fired multiple times for same state when i press volume down or up button.
add in androidmanifest.xml:
<receiver android:name=".VolumeSateReceiver">
<intent-filter>
<actionandroid:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
in VolumeSateReceiver.java:
public class VolumeSateReceiver extends BroadcastReceiver {
Context pcontext;
@Override
public void onReceive(Context context, Intent intent) {
pcontext = context;
//check the intent something like:
if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
int newVolume = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_VALUE", 0);
int oldVolume = intent.getIntExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE", 0);
if (newVolume != oldVolume) {
//Toast.makeText(pcontext ,"newVolume" +newVolume + " oldVolume" + oldVolume, Toast.LENGTH_SHORT).show();
System.out.println("In onReceive" + "newVolume" +newVolume + " oldVolume" + oldVolume );
}
}
}
}