setRingerMode to normal not working - android

I have seen a lot of questions about ringer mode. Nothing seems to give me a clue on the problem I am facing. I have set ringer mode to the normal RING mode for a service that gets started in the foreground. Unfortunately that doesnt seem to change the phone's ringer mode if it was on silent or vibrate in the first place. However, if put the same piece of code behind a button on an activity then it works.
The above described case happens only in Android 4.0.4 (ICS) but not in FR 2.2. Any clue on how to get around this? Here's my code:
AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

I have figured out why it didnt work on my phone. I had an other app called Flip Silent that interfered with the working of my app. After everytime I set the Audio Manager to ringer mode, it's service set it it back to the exisiting mode leading me to think it wasnt working.
it was difficult to debug as the logs didnt lead me in any direction. But a flicker on my home screen lead me to find this out. I uninstalled the flip silent app and everything worked!!

I also face the same problem so, I slove that this way.
int getMediaValue = audioManager.getStreamVolume(audioManager.STREAM_RING);
int setVolume = getMediaValue - 1;
// This was work for ring volume
audioManager.setStreamVolume(audioManager.STREAM_RING, setVolume, AudioManager.FLAG_SHOW_UI );
So you can try this way :)

Related

Handling volume change on android lockscreen?

What I am trying to do is, being able to catch volume up/down button actions on lockscreen on android 4.4.
Google Cast Design Checklist document describes lock screen requirement "Provide access to the volume control via hardware buttons". I tried various ways to handle hardware volume buttons on lock screen but none of them worked.
onKeyDown/dispatchKeyEvent - I tried to override onKeyDown as well as dispatchKeyEvent methods on Activity, but none of these are executed on lockscreen, these only works when my app is focused.
Settings.System.CONTENT_URI/ContentObserver - Registering content observer on main activity's content resolver does catch the system settings change, but that also does not occur on lockscreen.
android.intent.action.MEDIA_BUTTON - Having this filter in manifest, I am able to receive play/pause actions from lockscreen however no volume change event.
android.media.VOLUME_CHANGED_ACTION - Having this filter in manifest, this event is received in my BroadcastReceiver, unfortunately its extra values never get changed on lockscreen. When I keep hitting the volume button, the returned android.media.EXTRA_VOLUME_STREAM_VALUE remains the same (i.e. always 1), even though the CHANGED action is received by my broadcast receiver.
CastVideos-android - The reference android sender app seems to be able to control volume on receiver even when controling from senders lock screen, however even after putting breakpoitns all over the place around Cast.CastApi.setVolume(), these would not get picked. So it seems that the command is being send to a receiver from somewhere I can not locate.
I can also see some other apps being able to catch HW volume keys i.e. Play Music app. So my device surely is capable...
Can anyone suggest any working solution?
You either need to use RemoteControlClient (on pre-lollipop) or MediaSession that is introduced recently. For an example, look at CastCompanionLibrary, VideoCastManager#setUpRemoteControl() method where RCC is registered with the MediaRouter. If using MediaSession, then you need to register MediaSession with MediaRouter.
At the end it appeared I was missing one line of code:
MediaRouter.getInstance(context).addRemoteControlClient(remoteControlClient);
or
MediaRouter.getInstance(activity).setMediaSession(session.getMediaSession());
here is some more code from my implementation for 4.0+:
import android.media.AudioManager;
import android.media.RemoteControlClient;
import android.support.v7.media.MediaRouter;
remoteControlClient = new RemoteControlClient(pendingIntent);
remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.registerMediaButtonEventReceiver(receiver);
audioManager.registerRemoteControlClient(remoteControlClient);
MediaRouter.getInstance(context).addRemoteControlClient(remoteControlClient);
and for 5.0+:
import android.media.AudioManager;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.media.MediaRouter;
session = new MediaSessionCompat(activity, TAG);
session.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
MediaRouter.getInstance(activity).setMediaSession(session.getMediaSession());
The interesting thing is, there is some black magic that controls the receiver volume internally and your own Cast.CastApi.setVolume() call is not involved at all

Touch event UP being fired when not supposed to, after using "PROXIMITY_SCREEN_OFF_WAKE_LOCK" wakelock

I'm having a really weird issue here.
(Running on device,Motorola XT910 Android 4.1.2, Xamarin.Android 4.16.0)
I have an activity with a button that the user presses an holds to record some audio. I handle the onTouch event and listen to the DOWN and UP events (on DOWN I start recording, on UP I stop it). So far so good.
Now, I also handle the onSensorChanged event in order to turn the screen off when the user holds up the device to his ear. I achieve this by using a wakelock like this:
private void AcquireScreenWakeLock()
{
if (screenWakeLock == null) {
PowerManager pm = (PowerManager)GetSystemService (Context.PowerService);
field = pm.Class.GetField ("PROXIMITY_SCREEN_OFF_WAKE_LOCK").GetInt (null);
screenWakeLock = pm.NewWakeLock ((WakeLockFlags)field, "ScreenWakeLock");
}
screenWakeLock.Acquire();
}
private void ReleaseScreenWakeLock()
{
if (screenWakeLock == null)
return;
if (screenWakeLock.IsHeld)
screenWakeLock.Release();
}
The problem comes when pressing and holding the button to record, right after the DOWN event, I receive an UP event WITHOUT lifting my finger...
Of course there are other things that I do in between but I managed to narrow the problem down to this code (if I comment it out everything works as expected).
I know I'm using a hidden field to make the wakeLock (actually it seems it's released in API 21, docs here) but I haven't found a better way to do this, and the whole screen off/on process works flawlessly.
I'm having this issue while testing on a Motorola XT910 running Android 4.1.2, I've tested it on a Moto G running Android 4.4.4 and everything works fine there (no UP events being fired up when holding down the button).
I'm really at a loss here figuring this out...how can a wakelock cause this behaviour? Do you have any other way to effectively turn the screen off/on? I've searched other posts but none seemed to help me. Using a partial wakeLock doesn't work either (I've checked with this code).
Thanks in advance!
P.S: I've checked and the touch event is only being registered once.
PROXIMITY_SCREEN_OFF_WAKE_LOCK turns screen off/on according to proximity state by itself. No need to handle also onSensorChanged event. This might cause an issue.

Turn off ring tone temporarily

When someone calls, my app will turn off the ringing tone in order not to distract the user. So could you please tell me how to turn off the ringing sound in Android?
EDIT:
It seems I will need to use this one :http://developer.android.com/reference/android/media/Ringtone.html
I think I've found the solution. In case someone else needs it, I copy my solution here:
AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
There is a manager class called AudioManager which controls the audios and such. And it has a instance method setRingerMode(int mode) which accepts a integer flag.
Once you're done with the silence, you can change the status of the ring tone by passing in this flag RINGER_MODE_NORMAL

Android, Silent mode notification

In my android app I need to know, whenever the user is in Phone options mode (the one that appears when you hold down the power button for a while), and pushes the 'Silent mode' button. I have found that Airplane mode is linked to the ACTION_AIRPLANE_MODE_CHANGED. But I can not find any action event for the 'Silent mode' button?
The AudioManager provides a getRingerMode() method which can be used to determine the current state.
In your case you have to query the returned value for AudioManager.RINGER_MODE_SILENT, so something like
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (am.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
// do something neat here
}
In combination with AudioManager's RINGER_MODE_CHANGED_ACTION this should work for you

How to implement Phonestatelistener in an activity

I have certain data which i got, when user fill a form in an activity. There is an option Mode where user can select RING or VIBRATE.
So my question is how could i actually implement it in my activity, I see various examples on Telephony manager and phonestatelistener
http://marakana.com/forums/android/examples/62.html.
http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html
but its hard to implement it in my application,i have data in text form only and dont know how to use this data to switch it from one mode to another. Please tell me in terms of coding example.
phone state listener doesnt determine the ring or vibrate mode - it is for the call status (idle, ring, offhook)
to get the ring/vibrate/silent stau use Audiomanager
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.getRingerMode();// returns AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_SILENT or AudioManager.RINGER_MODE_NORMAL
// to set ringer mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

Categories

Resources