下面列出了android.provider.Settings#SettingNotFoundException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
private boolean isScreenAutoRotate() {
Context context = mContextRef.get();
if (context == null) {
return false;
}
int gravity = 0;
try {
gravity = Settings.System.getInt(context.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return gravity == 1;
}
public static boolean checkAccessibility(String service) {
int ok = 0;
try {
ok = Settings.Secure.getInt(AppApplication.getContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
}
TextUtils.SimpleStringSplitter ms = new TextUtils.SimpleStringSplitter(':');
if (ok == 1) {
String settingValue = Settings.Secure.getString(AppApplication.getContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
ms.setString(settingValue);
while (ms.hasNext()) {
String accessibilityService = ms.next();
if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}
return false;
}
protected static boolean isAccessibilitySettingsOn(Context context, final String service) {
int accessibilityEnabled = 0;
try {
accessibilityEnabled = Settings.Secure.getInt(
context.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
//
}
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(
context.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = new TextUtils.SimpleStringSplitter(':');
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
if (accessabilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}
return false;
}
public static boolean isGpsOpen(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
/**
* 判断是否开启了自动亮度调节
*
* @return
*/
public static boolean isAutoBrightness(Activity activity) {
boolean autoBrightness = false;
try {
autoBrightness = Settings.System.getInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return autoBrightness;
}
/**
* Increase screen brightness by a certain rate
* @param context
* @param currentImageBrightness
*/
private static synchronized void setIncreasedScreenBrightness(Context context, double currentImageBrightness){
try {
int currentScreenBrightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
int increasedScreenBrightness = currentScreenBrightness + SCREEN_BRIGHTNESS_INCREASE_RATE;
if (increasedScreenBrightness <= SCREEN_BRIGHTNESS_MAX){
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, increasedScreenBrightness);
Log.i(DetectionHelper.class.getName(), "setIncreasedScreenBrightness: Screen brightness has been increased: currentImageBrightness: " + currentImageBrightness + " currentScreenBrightness: " + currentScreenBrightness + " increasedScreenBrightness: " + increasedScreenBrightness);
}
} catch (Settings.SettingNotFoundException e) {
Log.e(DetectionHelper.class.getName(), null, e);
}
}
/**
* {@inheritDoc}
*/
public boolean isGranted() {
final ContentResolver cr = mContext.getContentResolver();
try {
int r = Settings.Secure.getInt(cr, Settings.Secure.ACCESSIBILITY_ENABLED);
if (r != 1) return false;
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "Accessibility enabled setting not found!");
}
final String flat = Settings.Secure.getString(cr,
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return flat != null && flat.contains(mComponentString);
}
/**
* 检查辅助服务是否打开
*
* @param mContext 1
* @return boolean
*/
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public static boolean isAccessibilitySettingsOn(@NonNull Context mContext) {
//注意:不要使用以下注释掉的代码取无障碍服务开启状态!disableSelf()之后仍返回true
//return ((AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled();
new DebugLog("isAccessibilitySettingsOn", DebugLog.LogLevel.V);
int accessibilityEnabled = 0;
final String service = mContext.getPackageName() + "/" + UnRootAccessibility.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException ignored) {
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}
return false;
}
private int getScreenBrightness() {
int value = 0;
ContentResolver cr = MApplication.getInstance().getContentResolver();
try {
value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException ignored) {
}
return value;
}
@Override
public int getInt(@NonNull String key) throws Settings.SettingNotFoundException {
/* Android's Settings class does this mapping from location mode to providers.
* This class bypasses the Settings class, we have to do that here too. */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.Q
&& Settings.Secure.LOCATION_MODE.equals(key)) {
return locationModeSetting.getLocationMode();
}
return super.getInt(key);
}
@Test
public void test_shouldFirstCallIsLocationModeOn_whenVersionKitKatOrAboveAndGeoActivated() throws Settings.SettingNotFoundException {
//noinspection WrongConstant
Mockito.when(contextMock.getSystemService(Mockito.eq(Context.LOCATION_SERVICE))).thenReturn(locationManagerMock);
Mockito.when(geoHelperSpy.isLocationModeOn(context)).thenReturn(true);
Mockito.when(geoHelperSpy.isKitKatOrAbove()).thenReturn(true);
PreferenceHelper.saveBoolean(context, MobileMessagingProperty.GEOFENCING_ACTIVATED, true);
// When
geoEnabledConsistencyReceiverWithSpy.onReceive(context, providersChangedIntent);
// Then
Mockito.verify(geoHelperSpy, Mockito.times(1)).isLocationModeOn(context);
Mockito.verify(geoHelperSpy, Mockito.never()).isNetworkProviderAvailable(context);
}
public static boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = mContext.getPackageName() + "/" + AccessibilityService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
} else {
return false;
}
return false;
}
/**
* Increase screen brightness by a certain rate
* @param context
* @param currentImageBrightness
*/
private static synchronized void setIncreasedScreenBrightness(Context context, double currentImageBrightness){
try {
int currentScreenBrightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
int increasedScreenBrightness = currentScreenBrightness + SCREEN_BRIGHTNESS_INCREASE_RATE;
if (increasedScreenBrightness <= SCREEN_BRIGHTNESS_MAX){
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, increasedScreenBrightness);
Log.i(DetectionHelper.class.getName(), "setIncreasedScreenBrightness: Screen brightness has been increased: currentImageBrightness: " + currentImageBrightness + " currentScreenBrightness: " + currentScreenBrightness + " increasedScreenBrightness: " + increasedScreenBrightness);
}
} catch (Settings.SettingNotFoundException e) {
Log.e(DetectionHelper.class.getName(), null, e);
}
}
/**
* Returns whether the user has enabled installing apps from sources other than the Google Play
* Store.
*/
private static boolean installingFromUnknownSourcesAllowed(Context context) {
try {
return Settings.Secure.getInt(
context.getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS)
== 1;
} catch (Settings.SettingNotFoundException e) {
return false;
}
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
protected boolean isDontKeepActivities() throws Settings.SettingNotFoundException {
try {
int val;
if (Build.VERSION.SDK_INT > 16) {
val = Settings.System.getInt(activity.getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES);
} else {
val = Settings.System.getInt(activity.getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES);
}
return val != 0;
} catch (Exception e) {
return false;
}
}
public static boolean isAccessibilityEnabled(Context context){
int accessibilityEnabled = 0;
try {
accessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Mlog.d(logTag, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Mlog.d(logTag, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled==1){
String settingValue = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Mlog.d(logTag, "Setting: " + settingValue);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Mlog.d(logTag, "Setting: " + accessibilityService);
if (accessibilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)){
Mlog.d(logTag, "We've found the correct setting - accessibility is switched on!");
return true;
} else if (Build.VERSION.SDK_INT < 18 && "com.pushbullet.android/com.pushbullet.android.notifications.mirroring.CompatNotificationMirroringService".equals(accessibilityService)) {
// For easier translation in case of other troublesome services
Toast.makeText(context, String.format(context.getString(R.string.accessibility_service_blocked),
"PushBullet Notification Mirroring"), Toast.LENGTH_LONG).show();
}
}
}
}
else{
Mlog.d(logTag, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
private void initBrightnessTouch() {
// if (!(getContext() instanceof Activity)) {
// return;
// }
Activity activity = fragActivity;//(Activity) getContext();
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
float brightnesstemp = lp.screenBrightness != -1f ? lp.screenBrightness : 0.6f;
// Initialize the layoutParams screen brightness
try {
if (Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
if (!Permissions.canWriteSettings(activity)) {
return;
}
Settings.System.putInt(activity.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
// restoreAutoBrightness = android.provider.Settings.System.getInt(activity.getContentResolver(),
// android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
} else if (brightnesstemp == 0.6f) {
brightnesstemp = android.provider.Settings.System.getInt(activity.getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS) / 255.0f;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
lp.screenBrightness = brightnesstemp;
activity.getWindow().setAttributes(lp);
mIsFirstBrightnessGesture = false;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public boolean isLocationModeOn(Context context) throws Settings.SettingNotFoundException {
int locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}
private static void switchRotation() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(!checkPermissionWrite()){
return;
}
}
//if(Build.VERSION.SDK_INT < 23) {
ContentResolver resolver = context.getContentResolver();
int gravity = -1;
try {
gravity = Settings.System.getInt(context.getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
if (gravity == 0) {
//打开
Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, 1);
MyApplication.getMainThreadHandler().post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"屏幕旋转已启用",Toast.LENGTH_SHORT).show();
}
});
} else if (gravity == 1) {
//关闭
Settings.System.putInt(resolver, Settings.System.ACCELEROMETER_ROTATION, 0);
MyApplication.getMainThreadHandler().post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"屏幕旋转已关闭",Toast.LENGTH_SHORT).show();
}
});
}
}