-
-
Notifications
You must be signed in to change notification settings - Fork 797
/
DeviceStatusUtils.java
359 lines (332 loc) · 12.1 KB
/
DeviceStatusUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package com.github.lazylibrary.util; /**
* Copyright 2014 Zhenguo Jin (jinzhenguo1990@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.annotation.TargetApi;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
/**
* 手机状态工具类 主要包括网络、蓝牙、屏幕亮度、飞行模式、音量等
*
* @author zhenguo
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public class DeviceStatusUtils {
/**
* Don't let anyone instantiate this class.
*/
private DeviceStatusUtils() {
throw new Error("Do not need instantiate!");
}
/**
* 获取系统屏幕亮度模式的状态,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @return System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC:自动;System.
* SCREEN_BRIGHTNESS_MODE_AUTOMATIC
* :手动;默认:System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
*/
public static int getScreenBrightnessModeState(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
/**
* 判断系统屏幕亮度模式是否是自动,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @return true:自动;false:手动;默认:true
*/
public static boolean isScreenBrightnessModeAuto(Context context) {
return getScreenBrightnessModeState(context) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC ? true
: false;
}
/**
* 设置系统屏幕亮度模式,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @param auto
* 自动
* @return 是否设置成功
*/
public static boolean setScreenBrightnessMode(Context context, boolean auto) {
boolean result = true;
if (isScreenBrightnessModeAuto(context) != auto) {
result = Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
auto ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
: Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
return result;
}
/**
* 获取系统亮度,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @return 亮度,范围是0-255;默认255
*/
public static int getScreenBrightness(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, 255);
}
/**
* 设置系统亮度(此方法只是更改了系统的亮度属性,并不能看到效果。要想看到效果可以使用setWindowBrightness()方法设置窗口的亮度),
* 需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @param screenBrightness
* 亮度,范围是0-255
* @return 设置是否成功
*/
public static boolean setScreenBrightness(Context context,
int screenBrightness) {
int brightness = screenBrightness;
if (screenBrightness < 1) {
brightness = 1;
} else if (screenBrightness > 255) {
brightness = screenBrightness % 255;
if (brightness == 0) {
brightness = 255;
}
}
boolean result = Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS, brightness);
return result;
}
/**
* 设置给定Activity的窗口的亮度(可以看到效果,但系统的亮度属性不会改变)
*
* @param activity
* 要通过此Activity来设置窗口的亮度
* @param screenBrightness
* 亮度,范围是0-255
*/
public static void setWindowBrightness(Activity activity,
float screenBrightness) {
float brightness = screenBrightness;
if (screenBrightness < 1) {
brightness = 1;
} else if (screenBrightness > 255) {
brightness = screenBrightness % 255;
if (brightness == 0) {
brightness = 255;
}
}
Window window = activity.getWindow();
WindowManager.LayoutParams localLayoutParams = window.getAttributes();
localLayoutParams.screenBrightness = (float) brightness / 255;
window.setAttributes(localLayoutParams);
}
/**
* 设置系统亮度并实时可以看到效果,需要WRITE_SETTINGS权限
*
* @param activity
* 要通过此Activity来设置窗口的亮度
* @param screenBrightness
* 亮度,范围是0-255
* @return 设置是否成功
*/
public static boolean setScreenBrightnessAndApply(Activity activity,
int screenBrightness) {
boolean result = true;
result = setScreenBrightness(activity, screenBrightness);
if (result) {
setWindowBrightness(activity, screenBrightness);
}
return result;
}
/**
* 获取屏幕休眠时间,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @return 屏幕休眠时间,单位毫秒,默认30000
*/
public static int getScreenDormantTime(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 30000);
}
/**
* 设置屏幕休眠时间,需要WRITE_SETTINGS权限
*
* @param context
* 上下文
* @param millis 时间
* @return 设置是否成功
*/
public static boolean setScreenDormantTime(Context context, int millis) {
return Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, millis);
}
/**
* 获取飞行模式的状态,需要WRITE_APN_SETTINGS权限
*
* @param context
* 上下文
* @return 1:打开;0:关闭;默认:关闭
*/
@SuppressWarnings("deprecation")
public static int getAirplaneModeState(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0);
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0);
}
}
/**
* 判断飞行模式是否打开,需要WRITE_APN_SETTINGS权限
*
* @param context
* 上下文
* @return true:打开;false:关闭;默认关闭
*/
public static boolean isAirplaneModeOpen(Context context) {
return getAirplaneModeState(context) == 1 ? true : false;
}
/**
* 设置飞行模式的状态,需要WRITE_APN_SETTINGS权限
*
* @param context
* 上下文
* @param enable
* 飞行模式的状态
* @return 设置是否成功
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @SuppressWarnings("deprecation")
public static boolean setAirplaneMode(Context context, boolean enable) {
boolean result = true;
// 如果飞行模式当前的状态与要设置的状态不一样
if (isAirplaneModeOpen(context) != enable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
result = Settings.System.putInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, enable ? 1 : 0);
} else {
result = Settings.Global.putInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0);
}
// 发送飞行模式已经改变广播
context.sendBroadcast(new Intent(
Intent.ACTION_AIRPLANE_MODE_CHANGED));
}
return result;
}
/**
* 获取蓝牙的状态
*
* @return 取值为BluetoothAdapter的四个静态字段:STATE_OFF, STATE_TURNING_OFF,
* STATE_ON, STATE_TURNING_ON
* @throws Exception
* 没有找到蓝牙设备
*/
public static int getBluetoothState() throws Exception {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (bluetoothAdapter == null) {
throw new Exception("bluetooth device not found!");
} else {
return bluetoothAdapter.getState();
}
}
/**
* 判断蓝牙是否打开
*
* @return true:已经打开或者正在打开;false:已经关闭或者正在关闭
* 没有找到蓝牙设备
*/
public static boolean isBluetoothOpen() {
int bluetoothStateCode = 0;
try {
bluetoothStateCode = getBluetoothState();
return bluetoothStateCode == BluetoothAdapter.STATE_ON
|| bluetoothStateCode == BluetoothAdapter.STATE_TURNING_ON ? true
: false;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 设置蓝牙状态
*
* @param enable
* 打开
* 没有找到蓝牙设备
*/
public static void setBluetooth(boolean enable) {
// 如果当前蓝牙的状态与要设置的状态不一样
if (isBluetoothOpen() != enable) {
// 如果是要打开就打开,否则关闭
if (enable) {
BluetoothAdapter.getDefaultAdapter().enable();
} else {
BluetoothAdapter.getDefaultAdapter().disable();
}
}
}
/**
* 获取铃声音量,需要WRITE_APN_SETTINGS权限
*
* @param context
* 上下文
* @return 铃声音量,取值范围为0-7;默认为0
*/
public static int getRingVolume(Context context) {
return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(AudioManager.STREAM_RING);
}
/**
* 获取媒体音量
*
* @param context
* 上下文
* @param ringVloume 音量
*/
public static void setRingVolume(Context context, int ringVloume) {
if (ringVloume < 0) {
ringVloume = 0;
} else if (ringVloume > 7) {
ringVloume = ringVloume % 7;
if (ringVloume == 0) {
ringVloume = 7;
}
}
((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_RING,
ringVloume, AudioManager.FLAG_PLAY_SOUND);
}
// /**
// * 找不到设备异常
// */
// public static class DeviceNotFoundException extends Exception{
// private static final long serialVersionUID = 1L;
//
// public DeviceNotFoundException(){}
//
// public DeviceNotFoundException(String message){
// super(message);
// }
// }
}