Observing Different Volume Streams and Keeping Fixed in Android Android by Rajesh Kumar Sahanee - February 4, 2019February 4, 20190 Post Views: 4,931 Hello Friends, Today I am going to share a way to observing different volume streams and keeping fixed in Android. There are different volume streams in Android i.e STREAM_MUSIC, STREAM_RING, STREAM_ALARM etc. We’ll use ContentObserver to detect change in volume. This code sample can be used where fixed volume needed i.e alarm app, phone finder app, theft warning app, etc. So Here is the code MainActivity.java MainActivity.java Java package com.zatackcoder.volumechangenotifier; import android.content.Context; import android.database.ContentObserver; import android.media.AudioManager; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { VolumeChangeObserver volumeChangeObserver; TextView textView; CheckBox checkBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textview); checkBox = findViewById(R.id.checkbox); volumeChangeObserver = new VolumeChangeObserver(getApplicationContext(), new Handler()); getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, volumeChangeObserver); } @Override protected void onDestroy() { super.onDestroy(); getApplicationContext().getContentResolver().unregisterContentObserver(volumeChangeObserver); } class VolumeChangeObserver extends ContentObserver { Context context; int musicVolume, ringVolume, alarmVolume; public VolumeChangeObserver(Context context, Handler handler) { super(handler); this.context = context; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); ringVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); alarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); } @Override public boolean deliverSelfNotifications() { return super.deliverSelfNotifications(); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int mVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); int rVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); int aVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); if(checkBox.isChecked()) { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0); audioManager.setStreamVolume(AudioManager.STREAM_RING, ringVolume, 0); audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmVolume, 0); return; } int mDelta = musicVolume - mVolume; int rDelta = ringVolume - rVolume; int aDelta = alarmVolume - aVolume; if (mDelta > 0) { textView.setText("Music Volume Decreased"); musicVolume = mVolume; } else if (mDelta < 0) { textView.setText("Music Volume Increased"); musicVolume = mVolume; } if (rDelta > 0) { textView.setText("Ring Volume Decreased"); ringVolume = rVolume; } else if (rDelta < 0) { textView.setText("Ring Volume Increased"); ringVolume = rVolume; } if (aDelta > 0) { textView.setText("Alarm Volume Decreased"); alarmVolume = aVolume; } else if (aDelta < 0) { textView.setText("Alarm Volume Increased"); alarmVolume = aVolume; } } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 package com.zatackcoder.volumechangenotifier; import android.content.Context;import android.database.ContentObserver;import android.media.AudioManager;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.TextView;import android.widget.Toast; public class MainActivity extends AppCompatActivity { VolumeChangeObserver volumeChangeObserver; TextView textView; CheckBox checkBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textview); checkBox = findViewById(R.id.checkbox); volumeChangeObserver = new VolumeChangeObserver(getApplicationContext(), new Handler()); getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, volumeChangeObserver); } @Override protected void onDestroy() { super.onDestroy(); getApplicationContext().getContentResolver().unregisterContentObserver(volumeChangeObserver); } class VolumeChangeObserver extends ContentObserver { Context context; int musicVolume, ringVolume, alarmVolume; public VolumeChangeObserver(Context context, Handler handler) { super(handler); this.context = context; AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); ringVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); alarmVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); } @Override public boolean deliverSelfNotifications() { return super.deliverSelfNotifications(); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int mVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); int rVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING); int aVolume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM); if(checkBox.isChecked()) { audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVolume, 0); audioManager.setStreamVolume(AudioManager.STREAM_RING, ringVolume, 0); audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmVolume, 0); return; } int mDelta = musicVolume - mVolume; int rDelta = ringVolume - rVolume; int aDelta = alarmVolume - aVolume; if (mDelta > 0) { textView.setText("Music Volume Decreased"); musicVolume = mVolume; } else if (mDelta < 0) { textView.setText("Music Volume Increased"); musicVolume = mVolume; } if (rDelta > 0) { textView.setText("Ring Volume Decreased"); ringVolume = rVolume; } else if (rDelta < 0) { textView.setText("Ring Volume Increased"); ringVolume = rVolume; } if (aDelta > 0) { textView.setText("Alarm Volume Decreased"); alarmVolume = aVolume; } else if (aDelta < 0) { textView.setText("Alarm Volume Increased"); alarmVolume = aVolume; } } }} activity_main.xml activity_main.xml XHTML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fix Volume" /> </LinearLayout> 12345678910111213141516171819202122232425 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fix Volume" /> </LinearLayout> Screenshots AndroidStudio Project Download Volume Change Notifier 1 file(s) 9.25 MB Download Please don’t forget to share if you find it useful