Android - Toggle Notification volume programmatically - android

A Checkbox found in the Ringer Volume settings that allows you
to set a separate Notification volume and incoming call volume
Is there a way to Check/Uncheck the Check box for Notification volume programmatically.

There is a hidden but editable preference in the Settings.System class that determines whether the notification volume is tied to the ringer volume.
It's called NOTIFICATIONS_USE_RING_VOLUME and is marked in the source code as hidden, with the well-commented reason that it will be removed from the platform at some point in the future.
The setting has an integer value of 0 if ringer and notification volumes are independent; 1 otherwise.

I don't recall seeing an API to do this, but you could try altering the notification (or ringer) volume programmatically to see whether that lets it "break free" of the ringer volume.
See AudioManager.adjustStreamVolume(STREAM_NOTIFICATION, ADJUST_RAISE, 0).

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

Android: Make app transparent and act on the background application

Is it feasible to make our Android application completely transparent (as if its not active at all) and work on the other apps?
My Requirement:
First last an app and after some few settings, make it transparent. Once its transparent, the user will not know that this app is active, however, our app should respond to only specific controls.
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast. So, currently I am using Power button which is not the requirement.
Please throw some light on this. I did some research but, couldnt find any. :(
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast.
I am not sure it this is right. If you read Android BroadCastReceiver for volume key up and down question, it seems that you can detect it in BroadCastRceiver. I've never tried but it might be worth a try. Do something like following:
In your BroadcastReceiver onReceive function, do something like following:
public void onReceive(Context arg0, Intent intent) {
if (intent!=null){
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
// Get the old volume from the SharedPOreferences
// volume variable contains the current volume
// Compare it to the old value saved. If it is greater than old value then user pressed the UP Volume button else DOWN volume.
}
}
Also I am not sure that you can make it transparent and still keep it running. You can have a background of an activity as transparent though. How do I create a transparent Activity on Android?. Hope it helps.

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