FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NormalCode/Utils.java at patch-1 · SolveBugs/NormalCode · GitHub
SolveBugs
NormalCode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NormalCode
/
Utils.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
418 lines (374 loc) · 14.6 KB
Breadcrumbs
NormalCode
/
Utils.java
Copy path
File metadata and controls
418 lines (374 loc) · 14.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* Created by zhen on 2016/8/19.
*/
/**
* 常用代码片段
*/
public
class
Utils
{
1.
给当前界面添加一个透明度
/**
* 给界面添加透明度
*
* @param activity
* @param bgAlpha
*/
public
static
void
backgroundAlpha
(
Activity
activity
,
float
bgAlpha
) {
activity
.
getWindow
().
addFlags
(
WindowManager
.
LayoutParams
.
FLAG_DIM_BEHIND
);
WindowManager
.
LayoutParams
lp
=
activity
.
getWindow
().
getAttributes
();
lp
.
alpha
=
bgAlpha
;
//0.0-1.0
activity
.
getWindow
().
setAttributes
(
lp
);
}
2.
一键添加qq群
//http://qun.qq.com/join.html 选择需要添加的群,然后选择手机平台,即可生成相应的代码。
3.
得到当前版本号
public
static
int
getVersionCode
(
Context
context
) {
int
code
=
0
;
if
(
context
==
null
) {
return
code
;
}
try
{
PackageInfo
info
=
context
.
getPackageManager
().
getPackageInfo
(
context
.
getPackageName
(),
0
);
code
=
info
.
versionCode
;
}
catch
(
PackageManager
.
NameNotFoundException
e
) {
}
return
code
;
}
4.
判断当前网络是否可用
public
static
boolean
isNetworkConnected
(
Context
context
) {
if
(
context
!=
null
) {
ConnectivityManager
mConnectivityManager
= (
ConnectivityManager
)
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
NetworkInfo
mNetworkInfo
=
mConnectivityManager
.
getActiveNetworkInfo
();
if
(
mNetworkInfo
!=
null
) {
return
mNetworkInfo
.
isAvailable
()
&&
mNetworkInfo
.
isConnectedOrConnecting
();
}
}
return
false
;
}
5.
当前是否是移动网络
public
static
boolean
isMobileConnected
(
Context
context
) {
if
(
context
!=
null
) {
ConnectivityManager
mConnectivityManager
= (
ConnectivityManager
)
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
NetworkInfo
mMobileNetworkInfo
=
mConnectivityManager
.
getNetworkInfo
(
ConnectivityManager
.
TYPE_MOBILE
);
if
(
mMobileNetworkInfo
!=
null
) {
return
mMobileNetworkInfo
.
isAvailable
()
&&
mMobileNetworkInfo
.
isConnectedOrConnecting
();
}
}
return
false
;
}
6.
当前是否是wifi
public
static
boolean
isWifi
(
Context
context
) {
if
(
context
!=
null
) {
ConnectivityManager
mConnectivityManager
= (
ConnectivityManager
)
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
);
NetworkInfo
mNetworkInfo
=
mConnectivityManager
.
getActiveNetworkInfo
();
if
(
mNetworkInfo
!=
null
) {
return
mNetworkInfo
.
getType
() ==
ConnectivityManager
.
TYPE_WIFI
;
}
}
return
false
;
}
7.
ListView根据item计算出实际的高度
public
static
void
setListViewHeightBasedOnChildren
(
ListView
listView
) {
// 获取ListView对应的Adapter
ListAdapter
listAdapter
=
listView
.
getAdapter
();
if
(
listAdapter
==
null
) {
return
;
}
int
totalHeight
=
0
;
for
(
int
i
=
0
;
i
<
listAdapter
.
getCount
();
i
++) {
View
listItem
=
listAdapter
.
getView
(
i
,
null
,
listView
);
listItem
.
measure
(
0
,
0
);
// 计算子项View 的宽高
totalHeight
+=
listItem
.
getMeasuredHeight
();
// 统计所有子项的总高度
}
ViewGroup
.
LayoutParams
params
=
listView
.
getLayoutParams
();
params
.
height
=
totalHeight
+ (
listView
.
getDividerHeight
() * (
listAdapter
.
getCount
() -
1
));
listView
.
setLayoutParams
(
params
);
}
8.
拨打电话
(
记得在清单文件添加权限
)
/**
* @param phoneNum
*/
public
void
call
(
String
phoneNum
,
Context
context
) {
Intent
intent
=
new
Intent
(
Intent
.
ACTION_CALL
,
Uri
.
parse
(
"tel:"
+
phoneNum
));
context
.
startActivity
(
intent
);
}
9.
字符串是否包含汉字
public
static
boolean
checkChinese
(
String
sequence
) {
final
String
format
=
"[
\\
u4E00-
\\
u9FA5
\\
uF900-
\\
uFA2D]"
;
boolean
result
=
false
;
Pattern
pattern
=
Pattern
.
compile
(
format
);
Matcher
matcher
=
pattern
.
matcher
(
sequence
);
result
=
matcher
.
find
();
//
return
result
;
}
10.
从assets
文件夹中读取图片
public
static
Drawable
loadImageFromAsserts
(
final
Context
ctx
,
String
fileName
) {
try
{
InputStream
is
=
ctx
.
getResources
().
getAssets
().
open
(
fileName
);
return
Drawable
.
createFromStream
(
is
,
null
);
}
catch
(
IOException
e
) {
if
(
e
!=
null
) {
e
.
printStackTrace
();
}
}
catch
(
OutOfMemoryError
e
) {
if
(
e
!=
null
) {
e
.
printStackTrace
();
}
}
catch
(
Exception
e
) {
if
(
e
!=
null
) {
e
.
printStackTrace
();
}
}
return
null
;
}
11.
判断字符串是否为空
public
static
boolean
isNull
(
String
string
) {
if
(
string
!=
null
) {
string
=
string
.
trim
();
if
(
string
.
length
() !=
0
) {
return
false
;
}
}
return
true
;
}
12.
递归删除文件夹及里边的文件
public
void
deletFiles
(
File
file
) {
if
(
file
.
isFile
()) {
file
.
delete
();
return
;
}
if
(
file
.
isDirectory
()) {
File
[]
childFile
=
file
.
listFiles
();
if
(
childFile
==
null
||
childFile
.
length
==
0
) {
file
.
delete
();
return
;
}
for
(
File
f
:
childFile
) {
deletFiles
(
f
);
}
file
.
delete
();
}
}
13.
非常全面的一个图片库
,
个人开发者必看
//http://www.iconfont.cn/collections
14.
各种单位转换
/**
* 获得屏幕宽度(像素)
*
* @param activity
* @return
*/
public
static
int
getDisplayWidth
(
Activity
activity
) {
DisplayMetrics
metric
=
new
DisplayMetrics
();
activity
.
getWindowManager
().
getDefaultDisplay
().
getMetrics
(
metric
);
int
width
=
metric
.
widthPixels
;
// 屏幕宽度(像素)
return
width
;
}
/**
* 获得屏幕高度(像素)
*
* @param activity
* @return
*/
public
static
int
getDisplayHeight
(
Activity
activity
) {
DisplayMetrics
metric
=
new
DisplayMetrics
();
activity
.
getWindowManager
().
getDefaultDisplay
().
getMetrics
(
metric
);
int
height
=
metric
.
heightPixels
;
// 屏幕高度(像素)
return
height
;
}
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
*
* @param context 通过上下文获得显示参数中的屏幕密度
* @param pxValue 需要转换的px值
* @return
*/
public
static
int
px2dip
(
Context
context
,
float
pxValue
) {
final
float
scale
=
context
.
getResources
().
getDisplayMetrics
().
density
;
return
(
int
) (
pxValue
/
scale
+
0.5f
);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
*
* @param context 通过上下文获得显示参数中的屏幕密度
* @param dipValue 需要转换的dp值
* @return
*/
public
static
int
dip2px
(
Context
context
,
float
dipValue
) {
final
float
scale
=
context
.
getResources
().
getDisplayMetrics
().
density
;
return
(
int
) (
dipValue
*
scale
+
0.5f
);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param context 通过上下文获得显示参数中的屏幕密度
* @param pxValue 需要转换的px值
* @return
*/
public
static
int
px2sp
(
Context
context
,
float
pxValue
) {
final
float
fontScale
=
context
.
getResources
().
getDisplayMetrics
().
scaledDensity
;
return
(
int
) (
pxValue
/
fontScale
+
0.5f
);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param context 通过上下文获得显示参数中的屏幕密度
* @param spValue 需要转换的sp值
* @return
*/
public
static
int
sp2px
(
Context
context
,
float
spValue
) {
final
float
fontScale
=
context
.
getResources
().
getDisplayMetrics
().
scaledDensity
;
return
(
int
) (
spValue
*
fontScale
+
0.5f
);
}
15.
压缩图片
,
防止Bitmap过大导致OOM
/**
* 谷歌推荐使用方法,从资源中加载图像,并高效压缩,有效降低OOM的概率
*
* @param res 资源
* @param resId 图像资源的资源id
* @param reqWidth 要求图像压缩后的宽度
* @param reqHeight 要求图像压缩后的高度
* @return
*/
public
Bitmap
decodeSampledBitmapFromResource
(
Resources
res
,
int
resId
,
int
reqWidth
,
int
reqHeight
) {
// 设置inJustDecodeBounds = true ,表示获取图像信息,但是不将图像的像素加入内存
final
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
();
options
.
inJustDecodeBounds
=
true
;
BitmapFactory
.
decodeResource
(
res
,
resId
,
options
);
// 调用方法计算合适的 inSampleSize
options
.
inSampleSize
=
calculateInSampleSize
(
options
,
reqWidth
,
reqHeight
);
// inJustDecodeBounds 置为 false 真正开始加载图片
options
.
inJustDecodeBounds
=
false
;
//将options.inPreferredConfig改成Bitmap.Config.RGB_565,
// 是默认情况Bitmap.Config.ARGB_8888占用内存的一般
options
.
inPreferredConfig
=
Bitmap
.
Config
.
RGB_565
;
return
BitmapFactory
.
decodeResource
(
res
,
resId
,
options
);
}
// 计算 BitmapFactpry 的 inSimpleSize的值的方法
public
int
calculateInSampleSize
(
BitmapFactory
.
Options
options
,
int
reqWidth
,
int
reqHeight
) {
if
(
reqWidth
==
0
||
reqHeight
==
0
) {
return
1
;
}
// 获取图片原生的宽和高
final
int
height
=
options
.
outHeight
;
final
int
width
=
options
.
outWidth
;
int
inSampleSize
=
1
;
// 如果原生的宽高大于请求的宽高,那么将原生的宽和高都置为原来的一半
if
(
height
>
reqHeight
||
width
>
reqWidth
) {
final
int
halfHeight
=
height
/
2
;
final
int
halfWidth
=
width
/
2
;
// 主要计算逻辑
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while
((
halfHeight
/
inSampleSize
) >=
reqHeight
&& (
halfWidth
/
inSampleSize
) >=
reqWidth
) {
inSampleSize
*=
2
;
}
}
return
inSampleSize
;
}
16.
请求打开蓝牙
//REQUEST_ENABLE_BT自定义requestCode
int
REQUEST_ENABLE_BT
=
0
;
Intent
enableBtIntent
=
new
Intent
(
BluetoothAdapter
.
ACTION_REQUEST_ENABLE
);
startActivityForResult
(
enableBtIntent
,
REQUEST_ENABLE_BT
) {
}
17.
读取Excel文件
使用jxl
jar包
。
18.
应用程序启动短暂白屏或者黑屏解决办法
(
启动页设置背景图片
)
//1.自定义全屏主题
<
style
name
="
FlashStyle
"
parent
=
"android:style/Theme.NoTitleBar.Fullscreen"
>
<
item
name
=
"android:windowBackground"
>
@
drawable
/
start_app_flash
</
item
>
</
style
>
//2.设置给闪屏页
android
:
theme
=
"@style/FlashStyle"
19.
判断是否是短时间内重复点击按钮之类的动作
(
防止重复打开页面等
)
private
static
long
lastClickTime
;
public
synchronized
static
boolean
isDoubleClick
(
long
time
) {
long
currentTime
=
System
.
currentTimeMillis
();
boolean
isClick2
;
if
(
currentTime
-
lastClickTime
>
time
) {
lastClickTime
=
currentTime
;
isClick2
=
false
;
}
else
{
isClick2
=
true
;
}
return
isClick2
;
}
20.
得到sd卡路径
public
static
String
getSDCardPath
() {
File
sdcardDir
=
null
;
boolean
sdcardExist
=
Environment
.
getExternalStorageState
().
equals
(
android
.
os
.
Environment
.
MEDIA_MOUNTED
);
if
(
sdcardExist
) {
sdcardDir
=
Environment
.
getExternalStorageDirectory
();
return
sdcardDir
.
toString
();
}
else
{
return
null
;
}
}
21
从网络路径下载图片保存到本地
,
并返回本地保存路径
public
static
String
getImageURI
(
String
networkPath
,
String
dirPath
,
String
s
) {
if
(
networkPath
==
null
) {
return
null
;
}
String
name
=
networkPath
.
substring
(
networkPath
.
lastIndexOf
(
"/"
));
File
dirFile
=
new
File
(
dirPath
);
if
(!
dirFile
.
exists
()) {
dirFile
.
mkdirs
();
}
File
file
=
new
File
(
dirFile
,
name
+
s
);
try
{
// 从网络上获取图片
URL
url
=
new
URL
(
networkPath
);
HttpURLConnection
conn
= (
HttpURLConnection
)
url
.
openConnection
();
conn
.
setConnectTimeout
(
5000
);
conn
.
setRequestMethod
(
"GET"
);
conn
.
setDoInput
(
true
);
if
(
conn
.
getResponseCode
() ==
200
) {
InputStream
is
=
conn
.
getInputStream
();
FileOutputStream
fos
=
new
FileOutputStream
(
file
);
byte
[]
buffer
=
new
byte
[
1024
];
int
len
=
0
;
while
((
len
=
is
.
read
(
buffer
)) != -
1
) {
fos
.
write
(
buffer
,
0
,
len
);
}
is
.
close
();
fos
.
close
();
// 返回一个URI对象
return
dirPath
+
File
.
separator
+
name
+
s
;
}
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
return
null
;
}
22
改变系统语言为英文环境
public
static
void
initLanguage
() {
try
{
Log
.
d
(
"Appcontext"
,
"初始化的时候设置系统语言为英文环境"
);
Class
<?>
activityManagerNative
=
Class
.
forName
(
"android.app.ActivityManagerNative"
);
Log
.
i
(
"amnType"
,
activityManagerNative
.
toString
());
Object
am
=
activityManagerNative
.
getMethod
(
"getDefault"
).
invoke
(
activityManagerNative
);
Log
.
i
(
"amType"
,
am
.
getClass
().
toString
());
Object
config
=
am
.
getClass
().
getMethod
(
"getConfiguration"
).
invoke
(
am
);
Log
.
i
(
"configType"
,
config
.
getClass
().
toString
());
config
.
getClass
().
getDeclaredField
(
"locale"
).
set
(
config
,
Locale
.
ENGLISH
);
config
.
getClass
().
getDeclaredField
(
"userSetLocale"
).
setBoolean
(
config
,
true
);
am
.
getClass
().
getMethod
(
"updateConfiguration"
,
android
.
content
.
res
.
Configuration
.
class
).
invoke
(
am
,
config
);
}
catch
(
Exception
e
) {
e
.
printStackTrace
();
}
}
23.
异或运算和校验
public
static
byte
getXor
(
byte
[]
datas
) {
byte
temp
=
datas
[
0
];
for
(
int
i
=
1
;
i
<
datas
.
length
;
i
++) {
temp
^=
datas
[
i
];
}
return
temp
;
}
}
Back
|
FazBrowse Home
|
New Git URL