On Android you have the setting "Control notifications on your lock screen" under Notifications, and to back that up you have the ability to set the visibility of your notification, as explained in the notification Tutorial.
My queston is then, is it possible to get the state of this setting?
The reason i would like the state of the setting is to give the user more options, if they have enabled content hiding, but not bother them if they haven't.
Yes, that's possible. There are two constants:
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"
However, since these values are not part of public API, they might change in future, or might not work on all devices.
int show_all = Settings.Secure.getInt(getContentResolver(), "lock_screen_allow_private_notifications", -1);
int noti_enabled = Settings.Secure.getInt(getContentResolver(), "lock_screen_show_notifications", -1);
if(show_all > 0 && noti_enabled > 0){
// Post notification
// ...
}
Related
The NotificationManager class in android lists two methods setInterruptionFilter (int interruptionFilter) and setNotificationPolicy (NotificationManager.Policy policy).
From the docs:
setInterruptionFilter
public final void setInterruptionFilter (int interruptionFilter)
Sets the current notification interruption filter.
The interruption filter defines which notifications are allowed to interrupt the user (e.g. via sound & vibration) and is applied globally.
setNotificationPolicy
public void setNotificationPolicy (NotificationManager.Policy policy)
Sets the current notification policy.
Both were added in API level 23.
From my understanding, both seem to accomplish the same task of setting the Do Not Disturb policy for the android device. What exactly is the difference between the two methods?
with setNotificationPolicy you can set the Do Not Disturb "Allow Interruptions" policies like NotificationManager.Policy.PRIORITY_CATEGORY_ALARM ( allow alarm )
with setInterruptionFilter you set the actual Don Not Disturb on or off where :
NotificationManager.INTERRUPTION_FILTER_PRIORITY = set DND on with the setNotificationPolicy settings
NotificationManager.INTERRUPTION_FILTER_NONE = set DND on for every interuption
NotificationManager.INTERRUPTION_FILTER_ALL = set DND off
TIP. You can get the policy first before changing it with NotificationManager.getNotificationPolicy() to set it back after your done.
I need to know if the Wifi settings pane is on top. I found this way:
ActivityManager a = (ActivityManager)context.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> t = a.getRunningTasks( 1 );
if ( t.get( 0 ).topActivity.getClassName().equals( "com.android.settings.Settings$WifiSettingsActivity" ) )
{
return true; // Yes it is!
}
else
{
return false; // No it's not!
}
Actually, it works fine. But in Android reference doc here it is written that we should not use this method (read the link for more details). So does somebody know how to perform this check in an other way? Note that I target SDK level 14 and higher.
Thanks!
Don't detect the settings pane, rather focus on why you would want to open the settings panel (you need WiFi connection and it is disabled, you need a particular SSID and the current one is not that one, ...).
For example, show your activity and if WiFi is disabled, show a popup to ask the user what he wants to do.
I have seen this question : check phone settings for haptic feedback but I don't understand the solution (for me it does not work).
Is there another solution ? I did not find anything on the javadoc.. ?
Thanks
You can add this method to any of your classes and use it to check whether haptic feedback is enabled.
public static boolean isHapticFeedbackEnabled(Context context) {
int enabled = Settings.System.getInt(context.getContentResolver(),
android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
return enabled != 0;
}
The method goes into system settings database and checks value of "haptic_feedback_enabled" setting. If there is 0 then haptic feedback is disables, otherwise it is enabled.
Issue
Finding methods to toggle between:
Always
Never
Only in Silent Mode
Only When Not in Silent Mode
These choices are found by the path --- Menu >> Settings >> Sound >> Vibrate --- on the phone.
It is simple to change by navigation on the phone (by the way, my phone is a Motorola Atrix 2 with Android 2.3.3), but I have yet to come across methods to use in my code.
Code
I basically have buttons that should manipulate the vibrate settings when clicked. One of these buttons is shown here:
bSilent.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
Toast.makeText(getBaseContext(), "Set to Never", Toast.LENGTH_SHORT).show();
}
});
audioManager is defined somewhere above this code as:
final AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
Android offers the AudioManager.setVibrateSetting, but it is now deprecated. Instead, they reference you to the getRingerMode method.
http://developer.android.com/reference/android/media/AudioManager.html
However, using these functions (and any combination of them) do not efficiently move between the four vibrate settings. For example, if I start at "Always", it is seemingly impossible for me to get to "Never". All combinations of vibrate methods will only move between "Always" and "Only in Silent Mode". On the other hand, if I start at "Never", the offered methods will only toggle between "Never" and "Only When Not in Silent Mode".
Therefore, suppose I want to have my phone in silent mode and want it to vibrate. Then, I decide I do not wish it to vibrate any longer. I am unable to switch from "Always" or "Only in Silent Mode" to "Never".
Past Solutions and Posts
I am aware that this is somewhat of a duplicate post on StackOverflow. The issue has been brought up before...
Here: Vibrate settings on Android 2.2
And (more recently) here: Changing vibrate setting
The former of the links provides an "answer". LuTHieR ends up in a discussion and eventually figures out a way on his own. He references the site:
https://android.googlesource.com/platform/packages/apps/Settings/+/froyo-release/src/com/android/settings/SoundSettings.java
and says "I looked at the source code of the com.android.settings.Settings class and copied part of the methods that enable and disable vibrate".
I looked through this site vigorously and could not find what he did. Could anyone clarify his solution?
Question
Does anyone have a way to precisely toggle between "Always", "Never", "Only in Silent Mode", and "Only When Not in Silent Mode"?
My solution (path of the function with income String sParam with needed mode of vibration set, refactoring if need to integer 0-3):
AudioManager audioManager = getSystemService( Context.AUDIO_SERVICE);
if( Build.VERSION.SDK_INT < 16)
{
// sParam may be:
// 0 - Always
// 1 - Never
// 2 - Only in silent mode (when sound is off)
// 3 - Only when not in silent mode (when sound is on)
if( (sParam.equals( "1") == true) || (sParam.equals( "3") == true))
{
Settings.System.putInt( Static.contextApplication.getContentResolver(), "vibrate_in_silent", 0);
if( sParam.equals( "1") == true)
audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
if( sParam.equals( "3") == true)
audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
}
if( (sParam.equals( "0") == true) || (sParam.equals( "2") == true))
{
Settings.System.putInt( Static.contextApplication.getContentResolver(), "vibrate_in_silent", 1);
if( sParam.equals( "0") == true)
audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
if( sParam.equals( "2") == true)
audioManager.setVibrateSetting( AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
}
}
// else (for new SDK > 16 via setRingerMode() ??? )
My app allows the user to access their corporate voice mail. Normally, durring a phone call when the user holds the device up to their ear, the screen shuts off so they wont accidentally push buttons with their face. I would like to make my app do the same thing when the user is listening to their voice mail.
anyone know how to do this?
If you are allowed to look at open source code without causing yourself problems, check the source of the Android Phone Application. Specifically src/com/android/phone/PhoneApp.java and src/com/android/phone/InCallScreen.java.
From src/com/android/phone/PhoneApp.java:
//Around line 519
// Wake lock used to control proximity sensor behavior.
if ((pm.getSupportedWakeLockFlags()
& PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK) != 0x0) {
mProximityWakeLock = pm.newWakeLock(
PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
LOG_TAG);
}
....
// Around line 1334
if (((state == Phone.State.OFFHOOK) || mBeginningCall)&& !screenOnImmediately) {
// Phone is in use! Arrange for the screen to turn off
// automatically when the sensor detects a close object.
if (!mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
mProximityWakeLock.acquire();
} else {
if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
}
} else {
// Phone is either idle, or ringing. We don't want any
// special proximity sensor behavior in either case.
if (mProximityWakeLock.isHeld()) {
if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: releasing...");
// Wait until user has moved the phone away from his head if we are
// releasing due to the phone call ending.
// Qtherwise, turn screen on immediately
int flags =
(screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
mProximityWakeLock.release(flags);
}
}
Additionally, if you look at the code for the PowerManager class, PROXIMITY_SCREEN_OFF_WAKE_LOCK is documented (but hidden) and should do what you want ( I am not sure which API level this works for, however ) -- but not in the table for some reason.
/**
* Wake lock that turns the screen off when the proximity sensor activates.
* Since not all devices have proximity sensors, use
* {#link #getSupportedWakeLockFlags() getSupportedWakeLockFlags()} to determine if
* this wake lock mode is supported.
*
* {#hide}
*/
public static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = WAKE_BIT_PROXIMITY_SCREEN_OFF;
If you aren't afraid of using a potential undocumented feature, it should do exactly what you need.
as of API level 21 (Lollipop) you can get proximity wake lock this just like that:
if(powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG);
wakeLock.setReferenceCounted(false);
return wakeLock;
} else {
return null;
}
}
then it is up to you to acquire and release the lock.
PS: PowerManager#getSupportedWakeLockFlags was hidden, but now exists nomore. They have invented isWakeLockLevelSupported instead.
Probably you don't need it anymore but for the ones that are interested in code you could have a look at my SpeakerProximity project at http://code.google.com/p/speakerproximity/
What you are seeing is the use of a proximity sensor. For devices that have one, you access it through SensorManager.