How can I detect if the volume is set to mute? - android

When I am developing an Android application, how can I detect if volume is set to mute?
How can my application get notified if the volume changes to mute or gets unmuted?

You can use AudioManager to check volume is mute or not mute.
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch( audio.getRingerMode() ){
case AudioManager.RINGER_MODE_NORMAL:
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
break;
}
and for Volume change there is BroadcastReceiver for that.
public class VolumeKeyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Implement Logic
}
}
Register the BroadcastReceiver
VolumeKeyReceiver keyReceiver = new VolumeKeyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
registerReceiver(keyReceiver, intentFilter);

Related

How to play sound when headphone connected in android programatically?

I am working on one of the project which need to play sound simultaneously when headphone is connected.
I am using below code but no luck
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
You need to use a BroadcastReceiver to handle the action sent when the headset is plugged. You need after that to check if the action sent by the broadcast equals Intent.ACTION_HEADSET_PLUG in the onReceive method, then you can play your sound using MediaPlayer
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
//Headset is unplugged
break;
case 1:
//Headset is plugged
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
Please see this answer https://stackoverflow.com/a/13610712/2354845

Detecting State changes made by Bluetooth adapter through a background service

I have been trying to develop a service that detects BLE devices in the background. The background service runs seemlesly when bluetooth is enabled. However when bluetooth is not running i turn on bluetooth via
bluetoothAdapter.enable();
In order to listen to changes made by the bluetooth adapter i declare a broadcastreceiver in my service in the onCreate() method like this:
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filter);
My broadcast receiver looks like this:
private final BroadcastReceiver receiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.v("Broadcast called", "in service");
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Log.v("Bluetooth state", "Off");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.v("Bluetooth state", "Turning Off");
break;
case BluetoothAdapter.STATE_ON:
Log.v("Bluetooth state", "Turned On");
scanLeDevice(true) ;
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.v("Bluetooth state", "Turning On");
break;
}
}
}
};
the adapter.enable() call turns bluetooth on successfully however the problem i am facing here is that the onReceive method in the receiver never gets triggered as a result of which i cannot run and scan for BLE devices.
When i do these exact same steps in an activity everything works perfectly.
At first i thought that the onReceive is not triggered because the service is in a background thread. So i also triggered the service via a WakefullBroadcastReceiver but that made no change in behavior.
I would like to understand what i am doing wrong here and some help in solving this.

AudioManager is unreliable

AudioManager is unreliable in onCallStateChanged. During a phone call I need it to turn on speaker phone and set the volume to max. It sometimes turns on speakerphone (usually during the second or later call) and rarely turns the volume up. My PhoneCallListener class is within my MainActivity class.
private class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
AudioManager aM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
aM.setMode(AudioManager.MODE_IN_CALL);
aM.setSpeakerphoneOn(true);
if(TelephonyManager.CALL_STATE_RINGING == state)
{
//phone ringing
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state)
{
//phone active
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
isPhoneCalling = true;
}
if(TelephonyManager.CALL_STATE_IDLE == state)
{
aM.setSpeakerphoneOn(false);
if(isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
Within CALL_STATE_OFFHOOK I had to turn off AudioManager.FLAG_SHOW_UI because it would continually show the volume UI. Also, setting aM.setStreamVolume(AudioManager.STREAM_MUSIC, aM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); crashes the app for some reason.
Any suggestions on how to make AudioManager work every time so that speakerphone is on and volume is max during a phone call?
Edit: Even with setting speackphoneon to true as soon as the onCallStateChanged method is called, it still is not reliably turning the speakerphone on. The volume is also unreliable and can't seem to set it to max without it crashing.
Below is the code to do this. I have tested in a phone running lollipop. Write your PhoneStateListener as:
private class myPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK: //Call is established
Log.d("s#urav", "Call is Offhook now!");
try {
Thread.sleep(500); //We never know when the call is actually OffHook
} catch (InterruptedException e) {
Log.d("s#urav","Exception is:"+e);
}
audioManager.setSpeakerphoneOn(true);
break;
case TelephonyManager.CALL_STATE_IDLE: //Call is finished
//Maintain a flag and do this only if speakerphone has been set on OFFHOOK
/*audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(false);*/
break;
}
}
}
For raising the volume of the call you have to increase the volume of STREAM_VOICE_CALL. This code + increasing the volume of call stream will meet you requirements.

Android User Inter Action with phone

I want to know if a user silent or unsilent the phone. How to detect user interaction with phone volume if user unsilent the phone my function will trigger
Thanks In Advance
You can detect whether the phone is in silent mode or not by using getRingerMode() method in AudioManager.
AudioManager mAudio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (mAudio.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
// your phone is in Silent Mode
break;
case AudioManager.RINGER_MODE_VIBRATE:
// your phone is in Vibrate Mode
break;
case AudioManager.RINGER_MODE_NORMAL:
// your phone is in Normal Mode
break;
}
Apply your logic with this code to determine when the mode of the phone gets changed and call your function accordingly.
EDIT : To detect change in the ringer mode, declare a RingerModeStateChangeReceiver which extends BroadcastReceiver and use this code inside onCreate() method of your Activity or Service that you want to process the broadcast:
BroadcastReceiver receiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
//call your function here
}
};
IntentFilter filter=new IntentFilter(
AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);

Ringer mode change listener Broadcast receiver?

AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("MyApp","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","Normal mode");
break;
}
From the code above I can get the ringer mode. What I would liek to do is listen the ringer mode changes and call a function.
What I have been told is that I can register the AudioManager. RINGER_MODE_CHANGED_ACTION and listen the change intent in broadcastreceiver onReceive method. It sounds clear. But I am new to android and really dont know how to write it. Is there any one can just write a piece of code and show how exactly it works instead of saying use this or that :) Thank you
Use the following code inside the onCreate() method of your Activity or Service that you want to process the broadcast:
BroadcastReceiver receiver=new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
//code...
}
};
IntentFilter filter=new IntentFilter(
AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);
Another solution is to add a receiver with an action in Manifest:
<receiver android:name=".receivers.RingerModeStateChangeReceiver" >
<intent-filter>
<action android:name="android.media.RINGER_MODE_CHANGED" />
</intent-filter>
</receiver>
and your class RingerModeStateChangeReceiver should extend BroadcastReceiver.
Here's an update version in Kotlin. Place this under your onCreate() lifecycle.
this.activity?.registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == AudioManager.RINGER_MODE_CHANGED_ACTION) {
// Set Player Volume
}
}
}, IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION))

Categories

Resources