Android系统开关

来源:互联网 发布:esp8266 51单片机 编辑:程序博客网 时间:2024/06/11 15:18

一、

亮度调节:

/** * 判断是否开启了自动亮度调节 *  * @param contentResolver * @return */public static boolean isAutoBrightness(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;}/** * 获取当前屏幕亮度 *  * @param activity * @return */public static int getScreenBrightness(Context context) {int nowBrightnessValue = 0;ContentResolver resolver = context.getContentResolver();try {nowBrightnessValue = Settings.System.getInt(resolver,Settings.System.SCREEN_BRIGHTNESS);} catch (SettingNotFoundException e) {e.printStackTrace();}return nowBrightnessValue;}/** * 设置亮度 *  * @param activity * @param brightness */public static void setBrightness(Activity activity, int brightness) {WindowManager.LayoutParams lp = activity.getWindow().getAttributes();lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);activity.getWindow().setAttributes(lp);}/** * ̬保存亮度设置状态 *  * @param resolver * @param brightness */public static void saveBrightness(Activity activity, int brightness) {ContentResolver resolver = activity.getContentResolver();Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");Settings.System.putInt(resolver, "screen_brightness", brightness);resolver.notifyChange(uri, null);}/** * ͣ 停止亮度自动调节 *  * @param activity */public static void stopAutoBtightness(Activity activity) {Settings.System.putInt(activity.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);}/** * 开启亮度自动调节 *  * @param activity */public static void startAutoBtightness(Activity activity) {Settings.System.putInt(activity.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);}
二、

飞行模式:

/** * 开启手机飞行模式设置页面 *  * @param context */@SuppressWarnings("deprecation")public static void setAirplaneModeOn(Context context) {if (getSystemVersion() >= 17) {Intent startActivityIntent = new Intent(Settings.ACTION_AIRPLANE_MODE_SETTINGS);startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if (startActivityIntent.resolveActivity(context.getPackageManager()) != null&& !CommonUtil.isLenoveK900()) {//Modified by wangyanpeng适配联想K900,通过上面的Intent跳转界面错误,所以直接跳转到设置界面context.startActivity(startActivityIntent);} else {Intent intent = new Intent(Settings.ACTION_SETTINGS);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if (intent.resolveActivity(context.getPackageManager()) != null) {context.startActivity(intent);}}} else {boolean enable = false;if (getAirplaneMode(context)) {enable = false;} else {enable = true;}Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0);Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);intent.putExtra("state", enable);context.sendBroadcast(intent);}}/** * 判断手机是否是飞行模式 *  * @param context * @return true:是 false:否 */public static boolean getAirplaneMode(Context context) {int isAirplaneMode = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,0);return (isAirplaneMode == 1) ? true : false;}
<span style="white-space:pre"></span>/**<span style="white-space:pre"></span> * 获取当前系统版本号<span style="white-space:pre"></span> * @return<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public static int getSystemVersion() {<span style="white-space:pre"></span>int version = android.os.Build.VERSION.SDK_INT;<span style="white-space:pre"></span>return version;<span style="white-space:pre"></span>}

三、

WIFI开关:

/** * 获取WIfimanager对象 *  * @param context * @return */private static WifiManager getWifiManager(Context context) {WifiManager wifiManager = (WifiManager) context.getSystemService(Service.WIFI_SERVICE);return wifiManager;}/** * 设置WIFI状态״̬ *  * @param context */public static void setWIFIState(Context context) {try {WifiManager wifiManager = getWifiManager(context);if (wifiManager.isWifiEnabled()) {wifiSwitchVisible = false;wifiManager.setWifiEnabled(false);} else {wifiSwitchVisible = true;wifiManager.setWifiEnabled(true);}} catch (Exception e) {}}/** * 获取当前手机是否连接到网络的状态 *  * @param context * @return */public static boolean checkNetworkState(Context context) {boolean flag = false;ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();if (activeNetworkInfo != null) {flag = activeNetworkInfo.isAvailable();}return flag;}/** * 获取״WIFI的状态 *  * @param context * @return */public static boolean isWifiEnabled(Context context) {WifiManager wifiManager = getWifiManager(context);return wifiManager.isWifiEnabled();}

四、

移动数据:

public static void setMobileDataStatus(Context context) {if (getSystemVersion() >= 21) {Intent intent = new Intent("/");ComponentName cm = new ComponentName("com.android.settings","com.android.settings.Settings$DataUsageSummaryActivity");intent.setComponent(cm);intent.setAction(CommonDefine.SYS_ACTION_BROWSER);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(intent);return;}ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);Class<?> cmClass = connManager.getClass();Class<?>[] argClasses = new Class[1];argClasses[0] = boolean.class;// 反射ConnectivityManager中hide的方法setMobileDataEnabled,可以开启和关闭GPRS网络Method method;try {method = cmClass.getMethod("setMobileDataEnabled", argClasses);if (getMobileDataStatus(context, "getMobileDataEnabled")) {method.invoke(connManager, false);} else {method.invoke(connManager, true);}} catch (Exception e) {e.printStackTrace();}}
<span style="white-space:pre"></span>/**<span style="white-space:pre"></span> * 获取移动数据开关状态<span style="white-space:pre"></span> * <span style="white-space:pre"></span> * @param context<span style="white-space:pre"></span> * @param getMobileDataEnabled<span style="white-space:pre"></span> * @return<span style="white-space:pre"></span> */<span style="white-space:pre"></span>public static boolean getMobileDataStatus(Context context,<span style="white-space:pre"></span>String getMobileDataEnabled) {<span style="white-space:pre"></span>ConnectivityManager cm;<span style="white-space:pre"></span>cm = (ConnectivityManager) context<span style="white-space:pre"></span>.getSystemService(Context.CONNECTIVITY_SERVICE);<span style="white-space:pre"></span>Class cmClass = cm.getClass();<span style="white-space:pre"></span>Class[] argClasses = null;<span style="white-space:pre"></span>Object[] argObject = null;<span style="white-space:pre"></span>Boolean isOpen = false;<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>Method method = cmClass.getMethod(getMobileDataEnabled, argClasses);<span style="white-space:pre"></span>isOpen = (Boolean) method.invoke(cm, argObject);<span style="white-space:pre"></span>} catch (Exception e) {<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>return false;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return isOpen;<span style="white-space:pre"></span>}

五、

蓝牙:

/** * 蓝牙开关 */public static void bluetoothSwitch() {try {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();if (isBluetoothOpen()) {bluetoothAdapter.disable();} else {bluetoothAdapter.enable();}} catch (Exception e) {}}/** * 判断蓝牙是否开启 *  * @return */public static boolean isBluetoothOpen() {BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();return bluetoothAdapter.isEnabled();}

GPS设置页面:

/** * 打开GPS设置页面 *  * @param context */public static void gpsSwitch(Context context) {Intent startActivityIntent = new Intent("android.settings.LOCATION_SOURCE_SETTINGS");startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);if (startActivityIntent.resolveActivity(context.getPackageManager()) != null) {context.startActivity(startActivityIntent);}}/** * 判断GPS是否打开 *  * @param context * @return */public static boolean isGPSOpen(Context context) {LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);}

七、

WIFI热点:

public static boolean setWifiApUnabled(Context context) {WifiManager wifiManager = getWifiManager(context);//wifiManager.setWifiEnabled(false);try {Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);return (Boolean) method.invoke(wifiManager, null, false);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}return false;}
八、

旋转卡关:

/** * 获取旋转开关状态 *  * @param context * @return */public static int getRotationStatus(Context context) {int status = 0;try {status = Settings.System.getInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION);} catch (SettingNotFoundException e) {e.printStackTrace();}return status;}





0 0
原创粉丝点击