"Do not keep activities " uncheck from code - android

Is there any way to uncheck the option "Do not keep activities " from developer settings in the code of my application? I want to check the status and uncheck this option for my application. Is this possible?

You should not rely on that setting, activities can be destroyed even if that setting is turned off (for instance, if the device is running low on memory).Also, you can not disable / enable the setting programatically.
You can, however, check if the setting is turned on, with this code:
boolean enabled;
if (Build.VERSION.SDK_INT >= 17) {
enabled = Settings.System.getInt(context.getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) == 1;
} else {
enabled = Settings.Global.getInt(context.getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) == 1;
}

Related

How to check if "Multiple users" is enabled

Is there a system setting table or API I can check to see if the "Multiple users" setting is turned on in Settings -> System -> Advanced -> Multiple users?
Thanks!
After a few hours of searching, I found the answer by browsing /data/system/users/0 and looking through settings_system.xml, settings_secure.xml, and settings_global.xml. It turns out what I'm looking for is "user_switcher_enabled" in settings_global.xml.
Here's my code:
public static boolean isMultipleUsersEnabled(Context context) {
try {
int pref = Settings.Global.getInt(context.getContentResolver(), "user_switcher_enabled");
return pref == 1 && (Build.VERSION.SDK_INT < Build.VERSION_CODES.N || UserManager.supportsMultipleUsers());
} catch (Settings.SettingNotFoundException e) {
Utils.logError(TAG, "user_switcher_enabled setting not found: " + e.toString());
return false;
}
}
The check for UserManager.supportsMultipleUsers() is probably not needed, but just in case the preference is somehow bogus on devices that don't actually support multiple users.
If there's anything you want to add to this answer, please comment below.
For API level 24 and above, you can use the method UserManager.supportsMultipleUsers(), which returns whether the device supports multiple users in boolean.
Before API level 24, there are no methods to check this without system permissions. There can be a workaround, like getting the count of users using the method getUserCount(). Again this also needs android.permission.MANAGE_USERS permission.

Get status of setting: "Control notifications on your lock screen"

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
// ...
}

How to check usb debugging enabled programmatically?

my question is on an Android phone how can I check if the usb debugging flag is enabled or not programmatically?
in my application I want to show the status of usb debugging and I want to get it programmatically
How can I get if usb debugging is enabled programmatically?
Try this:
if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ADB_ENABLED, 0) == 1) {
// debugging enabled
} else {
//;debugging does not enabled
}
Simple:
boolean isDebuggable = ( 0 != ( getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE ));
And if you want to check if it is connected:
if (!Debug.isDebuggerConnected()){
//Yes, it is.
}

Android test if haptic feedback is available

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.

Android: How to Toggle Between Vibrate Settings (Always, Never, Only in Silent Mode, Only When Not in Silent Mode)? Revisited

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() ??? )

Categories

Resources