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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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
Â
Please don’t forget to share if you find it useful
Comments