一、
亮度調節:
/**
* 判斷是否開啓了自動亮度調節
*
* @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;
}