I'm developing an app and need to completely turn off vibration alerts when my app is running. For example if an call comes in while my app is open I don't want to have the vibration alert, the same with emails and anyother notification.
So I've been googling around for a while and found this:
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,AudioManager.VIBRATE_SETTING_OFF);
The problem is that this method is deprecated. And I can't find an other way of doing this.
The minSdkVersion = 9 and targetSdkVersion = 17.
Any idea?
Thanks!!
See AudioManager RingerMode
It should now be
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Related
I have an alarm clock app and some users complain phone vibrates during alarm, while vibrations should be disabled.
After some digging I have found out, that on some devices there's a system option for device to vibrate along alarm music. For example in my test Pixel 4 it is located at Settings->Sound&Vibration->Vibration&Heptics->Alarm vibration.
What this setting, enabled by default, causes, is that vibrations try to "emulate" the music played through MediaPlayer and I cannot find a way to prevent that from happening from within the app or even detect if such setting is present/enabled.
Anyone knows how to get rid of that?
Here's a sample method I used for testing:
private fun startThePlayer(context: Context, playInLoop: Boolean = true) {
try {
mediaPlayer.reset()
mediaPlayer.isLooping = playInLoop
val uri = Settings.System.DEFAULT_RINGTONE_URI
mediaPlayer.setDataSource(context, uri)
mediaPlayer.setOnPreparedListener {
mediaPlayer.start()
}
mediaPlayer.prepareAsync()
} catch (e: Exception) {
log(e.toString())
}
}
VIBRATE permission is necessary for this to work.
Effect on Pixel 4 with Android 13:
Device is vibrating, as if trying to "emulate" the music played. Vibrations strength depends on value set in device's settings, completely ignoring volume set for alarm's music, and also messing up any vibrations set directly in my app.
What's interesting, is that for some reason Android's default clock app ignores this settings and device doesn't vibrate during it's alarms.
First, I suggest that you use android's VibratorManager to control your app vibrations settings (this is how the android development team called this service.. how bizarre). Use the getDefaultVibrator and cancel methods to stop any vibrations produced by your app (official docs to the rescue)
Second, because your app is an alarm clock please consider using the setWakeMode which will allow you to keep your app running in the background regardless to the display (in order to prevent the alarm from stopping if the screen is off). Here's the method documentation and also the PowerManager flags documentation
Hope you will find the right combination to satisfy your needs
I'm working on a VoIP app and when a call comes in, the ringtone is not audible on at least a Samsung A20e and a Samsung A71 device running on Android 11. Unfortunately, this info came from a few users who say they experience the issue and the issue doesn't arise on all the phones I have access to, so I'm not able to look in the logs myself.
To get the ringtone URI, the following code is used:
val uri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE)
Then that URI is used on the notification channel:
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
notificationChannel.setSound(uri, attributes)
And the URI is used once again in the method setSound of the NotificationCompat.Builder to generate the actual notification.
I tried to reproduce the issue on an emulator and changed the values in setUsage and setContentType a lot, but nothing seemed to have impact. I also reinstalled the app on every change, to make sure a new notification channel would be created.
Then I read somewhere on the internet that ringtones never work on an emulator. However, I do have an emulator on which the ringtone is actually audible, so I'm not so sure that's true, at least not for every configuration.
Lastly I should add that the ringtone also wasn't silent on my original emulator when I added the following line:
RingtoneManager.getRingtone(context, uri).play()
However, I'm not actually able to use that piece of code, since I need to attach the ringtone to a notification (channel).
Does anyone have experience with a same type of issue? Is there a problem with the settings of my notification or the notification channel? Is this just a Samsung issue? I hope someone can help me out.
I'll answer my own question. Turns out the client didn't describe the issue thorough enough and it turned out to be an easily solvable issue when he provided us with more information. The ringtone would be audible when the notification volume of the phone was on and the ringtone volume of the phone was off. However, when the notification volume was off and the ringtone volume was on, the ringtone wouldn't be audible. So the two of those got switched around, which got fixed by replacing AudioAttributes.USAGE_NOTIFICATION with AudioAttributes.USAGE_NOTIFICATION_RINGTONE.
I'm facing a Problem, related to my Question: Is it possible to ignore plugged in Headset and still use phone speakers?
I used the same code as in the linked question. The only change here is that I use the PhoneStateListener to intercept when a call is answered and respond to it. This Code is executed, if a Call get's answered:
AudioManager audioManager = (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
If I add audioManager.setSpeakerphoneOn(true); to it, the loudspeakers are activated (what I don't want)
Nothing happens if I change the AudioManager Mode from AudioManager.MODE_IN_CALL to AudioManager.MODE_IN_COMMUNICATION
My Problem is, that I can't make an "normal" Phone Call, while my Headset is plugged in.
So far I played around with AudioManager and the setMode() function of it.
Unfortunately, the only thing I managed to do was to make a phone call over the loudspeakers. But this is not the desired goal.
Can anyone help me with this?
My code used to successfully silence incoming calls by simply using setRingerMode, but ever since Android Pie, it's just not working anymore. I had tested the built-in DND mode, and it seemed to not be working either. But if that's true, it's working now, but my code still isn't.
Is there something additional necessary for this to work now? Android Pie does keep a separate mode from DND for ring, vibrate, and silence for ringer sounds, but I haven't been able to find figure out why my code isn't working anymore.
Update:
I'm using the following code:
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Update (1-23-19):
My understanding is I can't change the notification channel of another app, like the texting or calling app. Does anybody know any differently?
I would like to know whether we could receive the DTMF tones in android.Suppose,i am getting a call,and once i accept the call,is it possible to detect the keys that the other person is pressing during our call.I have go through many stack overflow questions regarding this,but most of them were not providing a solution.
How about implementing it through java reflection or something like that.?All the earlier post were for 2.2 and 3.0 versions.Presently we are in the 4.0 and above,so is it possible in the 4 or above versions?
Thanks in advance
There is no packages to do this in SDK.
FIRST, you need to listen the speaker voice, because you can not record voice call :
AudioManager mAudioManager = (AudioManager) Sos.getContext().getSystemService(Context.AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mAudioManager.setSpeakerphoneOn(true);
AND, this project may help you :
http://code.google.com/p/dtmf-decoder/source/checkout
I've done this, so i know it is possible this way.
Good luck