Dismiss the Keyguard without unlock sound - android

I am writing a replacement LockScreen, and using LayoutParams.DISSMISS_KEYGUARD as a Window flag is dismissing the Keyguard when switching on the screen and launching my LockScreen, however it always plays an lock sound when pressing the power button again, how can I surpress the lock sound?

you could use an AudioManager to turn the sound off in OnCreate then turn it back on later. You would also want to check if the sound is off to start so that you don't turn the sound on when it was off already
This to check sound state
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
case AudioManager.RINGER_MODE_VIBRATE:
silentMode = true;
break;
case AudioManager.RINGER_MODE_NORMAL:
silentMode = false;
break;
}
This to turn sound off
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
OR
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
This to turn sound on
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Of course you would probably make am a private variable so you don't need to declare it more than once. Like this.
public class MainActivity extends Activity {
//more variables
private AudioManager am;
private boolean silentMode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//check sound state and set silentMode;
//more stuff
}
}
I don't really know when to turn the sound back on but a place to try maybe after the call to getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

Related

Pause/Resume other/system's MediaPlayer for a While

I have a requirement in one of my app in which I need to play an audio for a while on click of a button.
Now there comes a case if user is already playing music on device, then i need to pause that music player to play my sound and after I release my MediaPlayer object I want to resume other app's(or atleast system's) music player.
What I have managed to do is to gain audio focus like below -
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (am!=null){
am.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener()
{
#Override
public void onAudioFocusChange(int focusChange) {
}},
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
But this snippet completely stops other app's music player and after releasing my MediaPlayer object AudioService is not giving focus back to system's music player.
You can use this code. For more info about onAudioFocusChangeListener
onAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS):
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT):
pause();
break;
case (AudioManager.AUDIOFOCUS_GAIN):
resume();
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
// Lower the volume while ducking.
player.setVolume(0.1f, 0.1f);
break;
}
}
};
int mediaresult = audioManager.requestAudioFocus(onAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (mediaresult == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//other app stop music you can play now
//put you play code here..
}
Use this :
public void musicPause() {
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive()) {
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause"); // you can give command here play,pause,resume and stop
playVideo.this.sendBroadcast(i); // playVideo is my Activity name
}
}

Android - PhoneStateListener onCallStateChanged() not working

I want to implement onCallStateChanged() of PhoneStateListener and use it's functionality to change audio settings when a call is received.
private final class CallStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
Utils.log("CallStateListener");
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Utils.log("Idle");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Utils.log("OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(prefs.getBoolean(Constants.PREF_OPTION_1, false)){
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxStreamValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
int volumeLevel = prefs.getInt(Constants.PREF_VOLUME_LEVEL, Constants.PREF_VOLUME_LEVEL_DEFAULT);
int volume = (int) Math.ceil(maxStreamValue * (volumeLevel / 100.0));
if(prefs.getBoolean(Constants.PREF_OPTION_3, false)){
Utils.log("Option3 true ringing");
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
Utils.changeNotificationVolume(audioManager, volume);
Utils.log("RINGING");
}
break;
}
callState = state;
}
}
I want to silent just the notifications if Option 1 and Option 3 are on, but a lots of new companies provide only one option for notifications and calls.
If Option 1 and Option 3 are on, i have set my device to SILENT MODE, but whenever a call is received i want my device to ring. Therefore in CALL_STATE_RINGING, i have my code to turn off SILENT MODE and ring the device.
This things only works sometime. After trying a lots of devices i have found out that usually this thing doesn't work on mobiles which lags a lot( are really slow).
I think this is because if audio settings are changed before mobile starts ringing this work, and if not this doesn't work even if settings are changed. Am i right? Is there any solution to this problem? Is there a way to delay ringing of android device? Or can i do something to make it work every time?

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

Android mute/unmute phone

My goal is to support 2 operations:
mute phone (possibly with vibrations enabled/disabled), so when a call or sms is received it won't make noise
unmute phone and restore the volume to the state before muting phone
How can I do this? What permissions are required in AndroidManifest?
This is the permission for vibrate into the manifest file
<uses-permission android:name="android.permission.VIBRATE" />
this is for to put the device in silent mode with vibrate
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
this is for to put into the ringing mode
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
public void changeRingerMode(Context context){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
/**
* To Enable silent mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
/**
* To Enable Ringer mode.....
*/
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
If what you want is to disable sound and restore the sound setting to previous state, this worked for me.
static int ringstate = 0;
private void soundOn(boolean off){
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if(off)
{ //turn off ringing/sound
//get the current ringer mode
ringstate = audio.getRingerMode();
if(ringstate!=AudioManager.RINGER_MODE_SILENT)
audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);//turn off
}
else
{
//restore previous state
audio.setRingerMode(ringstate);
}
}
This should do.

android get vibration settings

I need to get the right settings for vibrator so that my application respects the device's sound settings. On the following code, if the vibrator is off(on my phone when I lower the volume, it is a state when the volume is off and vibrator is off and one when volume is off an vibrator is on). When the phone is set to no vibrate (verified by making a call to this device), I still get the vibrator as being on:
AudioManager audioManager = (AudioManager) PingerApplication.getInstance().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
int vibrationSetting = audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION);
boolean vibrate;
switch(vibrationSetting) {
case AudioManager.VIBRATE_SETTING_ON:
vibrate = true;
break;
case AudioManager.VIBRATE_SETTING_OFF:
vibrate = false;
break;
case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
vibrate = (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
break;
default:
vibrate = false;
}
Am I doing something wrong? vibrationSetting is always AudioManager.VIBRATE_SETTING_ON
You can also put check on AudioManager.getRingerMode(), e.g.
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)
{
// should not vibrate
}
According to the Javadoc, you should use AudioManager.shouldVibrate(int) instead.

Categories

Resources