下面列出了怎么用android.provider.Settings.SettingNotFoundException的API类实例代码及写法,或者点击链接到github查看源代码。
public static boolean isLocationEnabled(Context context) {
if (VERSION.SDK_INT >= 19) {
try {
if (Secure.getInt(context.getContentResolver(), "location_mode") != 0) {
return true;
}
return false;
} catch (SettingNotFoundException e) {
e.printStackTrace();
return false;
}
} else if (TextUtils.isEmpty(Secure.getString(context.getContentResolver(), "location_providers_allowed"))) {
return false;
} else {
return true;
}
}
private int getUiOptionsAsFlags() {
int uiOptionsAsFlags = 0;
try {
ContentResolver contentResolver = mContext.getContentResolver();
int inversionEnabled = Settings.Secure.getInt(contentResolver,
Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
if (inversionEnabled == 1) {
uiOptionsAsFlags |= UI_OPTION_ACCESSIBILITY_INVERTED_FLAG;
}
float fontScale = Settings.System.getFloat(contentResolver,
Settings.System.FONT_SCALE);
if (fontScale > 1.0) {
uiOptionsAsFlags |= UI_OPTION_ACCESSIBILITY_MAGNIFIED_FLAG;
}
} catch (SettingNotFoundException e) {
Log.w(TAG, "Unexpected SettingNotFoundException");
}
return uiOptionsAsFlags;
}
public boolean locationServicesEnabled() throws PluginException {
Context context = getContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int locationMode = 0;
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} catch (SettingNotFoundException e) {
logger.error("Location services check failed", e);
throw new PluginException("Location services check failed", e, PluginException.SETTINGS_ERROR);
}
} else {
String locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
public static boolean isAccessibilitySettingsOn(Context context, String serviceName) {
int accessibilityEnabled = 0;
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(context.getApplicationContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (SettingNotFoundException e) {
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
if (accessabilityService.equalsIgnoreCase(serviceName)) {
return true;
}
}
}
} else {
// Log.v(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
/**
* Is Roaming enabled.
*
* @return
*/
@SuppressWarnings("deprecation")
private static boolean isRoamingEnabled(Context ctx) {
ContentResolver cr = ctx.getContentResolver();
int result = 0; // 0 is false
boolean check = false;
try {
result = Settings.Secure.getInt(cr, Settings.Secure.DATA_ROAMING);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
if (result == 1) {
check = true;
}
return check;
}
@TargetApi(android.os.Build.VERSION_CODES.FROYO)
private void initBrightnessTouch() {
float brightnesstemp = 0.6f;
// Initialize the layoutParams screen brightness
try {
if (AndroidUtil.isFroyoOrLater() &&
Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
mRestoreAutoBrightness = android.provider.Settings.System.getInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
} else {
brightnesstemp = android.provider.Settings.System.getInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
}
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightnesstemp;
getWindow().setAttributes(lp);
mIsFirstBrightnessGesture = false;
}
@Override
public boolean isBleScanAlwaysAvailable() {
if (isAirplaneModeOn() && !mEnable) {
return false;
}
try {
return Settings.Global.getInt(mContentResolver,
Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE) != 0;
} catch (SettingNotFoundException e) {
}
return false;
}
private boolean updateInputDeviceVibratorsLocked() {
boolean changed = false;
boolean vibrateInputDevices = false;
try {
vibrateInputDevices = Settings.System.getIntForUser(
mContext.getContentResolver(),
Settings.System.VIBRATE_INPUT_DEVICES, UserHandle.USER_CURRENT) > 0;
} catch (SettingNotFoundException snfe) {
}
if (vibrateInputDevices != mVibrateInputDevicesSetting) {
changed = true;
mVibrateInputDevicesSetting = vibrateInputDevices;
}
if (mVibrateInputDevicesSetting) {
if (!mInputDeviceListenerRegistered) {
mInputDeviceListenerRegistered = true;
mIm.registerInputDeviceListener(this, mH);
}
} else {
if (mInputDeviceListenerRegistered) {
mInputDeviceListenerRegistered = false;
mIm.unregisterInputDeviceListener(this);
}
}
mInputDeviceVibrators.clear();
if (mVibrateInputDevicesSetting) {
int[] ids = mIm.getInputDeviceIds();
for (int i = 0; i < ids.length; i++) {
InputDevice device = mIm.getInputDevice(ids[i]);
Vibrator vibrator = device.getVibrator();
if (vibrator.hasVibrator()) {
mInputDeviceVibrators.add(vibrator);
}
}
return true;
}
return changed;
}
private int runVibrate() {
Trace.traceBegin(Trace.TRACE_TAG_VIBRATOR, "runVibrate");
try {
try {
final int zenMode = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.ZEN_MODE);
if (zenMode != Settings.Global.ZEN_MODE_OFF) {
try (PrintWriter pw = getOutPrintWriter();) {
pw.print("Ignoring because device is on DND mode ");
pw.println(DebugUtils.flagsToString(Settings.Global.class, "ZEN_MODE_",
zenMode));
return 0;
}
}
} catch (SettingNotFoundException e) {
// ignore
}
final long duration = Long.parseLong(getNextArgRequired());
if (duration > MAX_VIBRATION_MS) {
throw new IllegalArgumentException("maximum duration is " + MAX_VIBRATION_MS);
}
String description = getNextArg();
if (description == null) {
description = "Shell command";
}
VibrationEffect effect =
VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE);
vibrate(Binder.getCallingUid(), description, effect, AudioAttributes.USAGE_UNKNOWN,
mToken);
return 0;
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIBRATOR);
}
}
private void incrementBootCount() {
synchronized (mLock) {
int count;
try {
count = Settings.Global.getInt(
getContext().getContentResolver(), Settings.Global.BOOT_COUNT);
} catch (SettingNotFoundException e) {
count = 0;
}
Settings.Global.putInt(
getContext().getContentResolver(), Settings.Global.BOOT_COUNT, count + 1);
}
}
private int getPointerSpeedSetting() {
int speed = InputManager.DEFAULT_POINTER_SPEED;
try {
speed = Settings.System.getIntForUser(mContext.getContentResolver(),
Settings.System.POINTER_SPEED, UserHandle.USER_CURRENT);
} catch (SettingNotFoundException snfe) {
}
return speed;
}
private int getShowTouchesSetting(int defaultValue) {
int result = defaultValue;
try {
result = Settings.System.getIntForUser(mContext.getContentResolver(),
Settings.System.SHOW_TOUCHES, UserHandle.USER_CURRENT);
} catch (SettingNotFoundException snfe) {
}
return result;
}
private static boolean isAccessibilityServiceRunning(Context context) {
boolean serviceRunning = false;
try {
ContentResolver contentResolver = context.getContentResolver();
int a11yEnabled = Settings.Secure.getInt(contentResolver,
Settings.Secure.ACCESSIBILITY_ENABLED);
if (a11yEnabled == 1) {
serviceRunning = true;
}
} catch (SettingNotFoundException e) {
Log.w(TAG, "Unexpected SettingNotFoundException");
e.printStackTrace();
}
return serviceRunning;
}
/**
* Returns whether the user has allowed AIDs registered in the
* specified category to be handled by a service that is preferred
* by the foreground application, instead of by a pre-configured default.
*
* Foreground applications can set such preferences using the
* {@link #setPreferredService(Activity, ComponentName)} method.
*
* @param category The category, e.g. {@link #CATEGORY_PAYMENT}
* @return whether AIDs in the category can be handled by a service
* specified by the foreground app.
*/
public boolean categoryAllowsForegroundPreference(String category) {
if (CATEGORY_PAYMENT.equals(category)) {
boolean preferForeground = false;
try {
preferForeground = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_FOREGROUND) != 0;
} catch (SettingNotFoundException e) {
}
return preferForeground;
} else {
// Allowed for all other categories
return true;
}
}
/**
* Returns maximum size, in bytes, of downloads that may go over a mobile connection; or null if
* there's no limit
*
* @param context the {@link Context} to use for accessing the {@link ContentResolver}
* @return maximum size, in bytes, of downloads that may go over a mobile connection; or null if
* there's no limit
*/
public static Long getMaxBytesOverMobile(Context context) {
try {
return Settings.Global.getLong(context.getContentResolver(),
Settings.Global.DOWNLOAD_MAX_BYTES_OVER_MOBILE);
} catch (SettingNotFoundException exc) {
return null;
}
}
@UsedForTesting
static boolean isInSystemSetupWizard(final Context context) {
try {
final int userSetupComplete = Settings.Secure.getInt(
context.getContentResolver(), Settings_Secure_USER_SETUP_COMPLETE);
return userSetupComplete == USER_SETUP_IS_NOT_COMPLETE;
} catch (final SettingNotFoundException e) {
Log.w(TAG, "Can't find settings in Settings.Secure: key="
+ Settings_Secure_USER_SETUP_COMPLETE);
return false;
}
}
public int getBrightness()
{
int mOldBrightness = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
mOldBrightness = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException snfe)
{
mOldBrightness = 255; }
return mOldBrightness;
}
public int getBacklightTime()
{
int Time = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
Time = Settings.System.getInt(cr, Settings.System.SCREEN_OFF_TIMEOUT);
}
catch (SettingNotFoundException snfe)
{
//Log.d(tag,"error()");
}
return Time;
}
public int getBluetoothStatus()
{
int iStatus = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
iStatus = Settings.Secure.getInt(cr, Settings.Secure.BLUETOOTH_ON);
}
catch (SettingNotFoundException snfe)
{
iStatus = 0; }
return iStatus;
}
public int getWiFiStatus()
{
int iStatus = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
iStatus = Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
}
catch (SettingNotFoundException snfe)
{
iStatus = 0; }
return iStatus;
}
private static boolean isAccessibilitySettingsOn(Context ctx) {
int accessibilityEnabled = 0;
final String service = ctx.getPackageName() + "/"
+ OversecAccessibilityService_1.class.getName();
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(ctx
.getApplicationContext().getContentResolver(),
Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(ctx
.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
String[] values = settingValue.split("\\:");
for (String string : values) {
if (string.equalsIgnoreCase(service)) {
return true;
}
}
}
}
return accessibilityFound;
}
public void playDtmf(ContentResolver r, char dtmf) {
try {
if (Settings.System.getInt(r, Settings.System.DTMF_TONE_WHEN_DIALING) == 0) {
// audible touch disabled: don't play on speaker, only send in outgoing stream
return;
}
} catch (SettingNotFoundException e) {}
getLc().playDtmf(dtmf, -1);
}
public int getBrightness()
{
int mOldBrightness = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
mOldBrightness = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
}
catch (SettingNotFoundException snfe)
{
mOldBrightness = 255; }
return mOldBrightness;
}
public int getBacklightTime()
{
int Time = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
Time = Settings.System.getInt(cr, Settings.System.SCREEN_OFF_TIMEOUT);
}
catch (SettingNotFoundException snfe)
{
//Log.d(tag,"error()");
}
return Time;
}
public int getBluetoothStatus()
{
int iStatus = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
iStatus = Settings.Secure.getInt(cr, Settings.Secure.BLUETOOTH_ON);
}
catch (SettingNotFoundException snfe)
{
iStatus = 0; }
return iStatus;
}
public int getWiFiStatus()
{
int iStatus = 0;
ContentResolver cr = mcontext.getContentResolver();
try
{
iStatus = Settings.Secure.getInt(cr, Settings.Secure.WIFI_ON);
}
catch (SettingNotFoundException snfe)
{
iStatus = 0; }
return iStatus;
}
@UsedForTesting
static boolean isInSystemSetupWizard(final Context context) {
try {
final int userSetupComplete = Settings.Secure.getInt(
context.getContentResolver(), Settings_Secure_USER_SETUP_COMPLETE);
return userSetupComplete == USER_SETUP_IS_NOT_COMPLETE;
} catch (final SettingNotFoundException e) {
Log.w(TAG, "Can't find settings in Settings.Secure: key="
+ Settings_Secure_USER_SETUP_COMPLETE);
return false;
}
}
private int getScreenBrightness() {
int screenBrightness = 255;
try {
screenBrightness = System.getInt(getActivity().getContentResolver(), "screen_brightness");
} catch (SettingNotFoundException e) {
}
return screenBrightness;
}
/**
* 获取当前屏幕亮度,范围0-255
*
* @param context
* @return 屏幕当前亮度值
*/
public static int getScreenBrightness(Context context) {
int rightnessValue = 0;
try {
rightnessValue = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return rightnessValue;
}
/**
* 判断是否开启了自动亮度调节
*
* @param context
* @return
*/
public static boolean isAutomicBrightness(Context context) {
boolean automicBrightness = false;
try {
automicBrightness = Settings.System.getInt(
context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
return automicBrightness;
}