I have an app of school hours and put the option to mute the device during the time stipulated by the user (beginning and end). However there is only the option to enable and disable this feature. How can I do this then?
Use AlarmManager to schedule times where you want mute or turn sound on in your phone, start here
and AudioManager to turn it on or off:
AudioManager am;
am= (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
//For Normal mode
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
//For Silent mode
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//For Vibrate mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
Related
I am trying to write an app to route audio output on speaker BUT leave microphone on wired headset when the headset is connected.
It is possible to route audio output on speaker when headset is connected with the following code:
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setWiredHeadsetOn(false);
audioManager.setSpeakerphoneOn(true);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
However, when I do this, headset microphone is also disabled.
But I need it to receive audio input.
How can I separately manage input and output devices ?
To the following question, there is a solution (at the bottom) but it is only valid if I start the mediaplayer.
In my case, I am trying to route the media of other apps to the speaker.
Route MIC input through headset and AUDIO output through phone's speaker
Is there any way to do it ?
I am building an app that uses the default alarm app to set alarms. When I used AlarmClock.EXTRA_SKIP_UI with setting alarms, it actually skipped the intent UI and set the alarm. However, when I use the same skip UI with ACTION_DISMISS_ALARM, the default alarm app pops up.
This is the code for setting the alarm:
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
intent.putExtra(AlarmClock.EXTRA_MESSAGE,"id:"+indexOfNote);
intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
intent.putExtra(AlarmClock.EXTRA_HOUR,alarmHour);
intent.putExtra(AlarmClock.EXTRA_MINUTES,alarmMin);
startActivity(intent);
and this is the code for dismissing the alarm:
Intent intent = new Intent(AlarmClock.ACTION_DISMISS_ALARM);
intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(AlarmClock.EXTRA_ALARM_SEARCH_MODE, AlarmClock.ALARM_SEARCH_MODE_LABEL);
intent.putExtra(AlarmClock.EXTRA_MESSAGE,"id:"+index );
startActivity(intent);
im building an app that uses the default alarm app to set alarms
There are dozens, if not hundreds, of alarm clock apps for Android that might honor these Intent actions. This includes both pre-installed alarm clock apps and user-installed alarm clock apps. There is no single "default alarm app".
but when i use the same skip ui with ACTION_DISMISS_ALARM the default alarm app pops up
EXTRA_SKIP_UI is not documented for use with ACTION_DISMISS_ALARM. So, what you are seeing is what I would expect.
As I am developing calling app in Android, I have achieved to play native ringing on call incoming by:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
ringtone.play();
so what I want is to achieve native whole behavior as when we receive a phone call or any other application's call.
(As I am beginner on this site and on android development so please guide me on my mistakes and thank you in advance)
try this
RINGER_MODE_NORMAL This mode is used to set ringing mode in device.
RINGER_MODE_SILENT This mode is used to set silent mode in device.
RINGER_MODE_VIBRATE This mode is used to set vibration mode in device.
try this code...
create an instance of AudioManager class by calling the getSystemService() method with an argument of Context.AUDIO_SERVICE.Once we create an instance of AudioManager class, we can use setRingerMode() method to set the volume or ringing modes of our device based on our requirements.
CODE:
AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
by using AudioManager class getRingerMode() method we can easily get the device current ringer mode.
AudioManager aManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int currentMode = aManager.getRingerMode();
if(currentMode == AudioManager.RINGER_MODE_NORMAL){
// Do your code
}
I am working on a VoIP app, I get an issue when user unplugs headphone then device automatically switches to loud speaker but I want device to switch to normal speaker. Is there any way I can change it to normal speaker?
Tricky stuff, esp multiple devices, different scenarios.
I have worked on a VoIP app that is how i know this. What you are facing is probably because the audio manager mode and-or stream is being handled manually after requesting focus. What you need to do is to transfer the control to the call stream so while your call is on,
connection and disconnection of headset, transfer to the phone earpiece etc is handled automatically by the o.s.
You could probably be doing all of this or a combination...
Use AudioManager {.setMode(int mode)} - use MODE_IN_COMMUNICATION, recommended for VoIP or MODE_IN_CALL
Also, check STREAM_VOICE_CALL
Are you usinng
- isSpeakerphoneOn() and setSpeakerphoneOn(boolean)
- isBluetoothScoOn(), setBluetoothScoOn(boolean) ?
Will need to play around with all of those for device or headset model specific issues.
Useful stuff:
Even if a SCO connection is established, the following restrictions
apply on audio output streams so that they can be routed to SCO
headset:
the stream type must be STREAM_VOICE_CALL
the format must be mono
the sampling must be 16kHz or 8kHz
To turn on earpiece programmatically try this:
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
private void useEarpiece() {
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(false);
}
Check your permission:
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
To catch event:
private class HeadSetReceiver extends BroadcastReceiver {
#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 unplugged
break;
case 1:
// Headset plugged in
break;
}
}
}
}
I am developing an android application for which i want to know whether the android phone is silent or not. I need to know how can I check this thing on android emulator by code.
You can use AudioManager class for your requirement,
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
{
// silent mode
}