- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPhoneUtil.java
More file actions
Latest commit
462 lines (426 loc) · 14.6 KB
/
Copy pathPhoneUtil.java
File metadata and controls
462 lines (426 loc) · 14.6 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
packagecom.realmo.utils;
importandroid.app.ActivityManager;
importandroid.app.Application;
importandroid.content.ClipData;
importandroid.content.ClipboardManager;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importandroid.content.pm.ApplicationInfo;
importandroid.content.pm.PackageInfo;
importandroid.content.pm.PackageManager;
importandroid.net.Uri;
importandroid.text.TextUtils;
importandroid.view.View;
importandroid.view.inputmethod.InputMethodManager;
importandroid.widget.EditText;
importandroid.widget.TextView;
importjava.net.Inet4Address;
importjava.net.InetAddress;
importjava.net.NetworkInterface;
importjava.net.SocketException;
importjava.text.DecimalFormat;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Enumeration;
importjava.util.List;
importjava.util.Locale;
/**
* 获取手机相关信息
*/
publicclassPhoneUtil {
/**
* 获取当前应用程序的包名
* @param context 上下文对象
* @return 返回包名
*/
publicstaticStringgetAppProcessName(Contextcontext) {
//当前应用pid
intpid = android.os.Process.myPid();
//任务管理类
ActivityManagermanager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//遍历所有应用
List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfoinfo : infos) {
if (info.pid == pid)//得到当前应用
returninfo.processName;//返回包名
}
return"";
}
/**
* 获取手机屏幕宽度
*/
publicstaticintgetPhoneWidth(Contextcontext) {
returncontext.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取手机屏幕高度
*/
publicstaticintgetPhoneHeight(Contextcontext) {
returncontext.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取控件高度
*/
publicstaticintgetWidgetHeight(Viewv) {
intw = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
inth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
returnv.getMeasuredHeight();
}
/**
* 获取控件宽度
*/
publicstaticintgetWidgetWidth(Viewv) {
intw = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
inth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
returnv.getMeasuredWidth();
}
/**
* dp 转 px
*/
publicstaticintdp2px(Contextcontext, floatdpValue) {
finalfloatscale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 转 dp
*/
publicstaticintpx2dp(Contextcontext, floatpxValue) {
finalfloatscale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 获取手机ip
*/
publicstaticStringgetLocalIpAddress() {
try {
for (Enumerationen = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterfaceni = (NetworkInterface) en.nextElement();
for (EnumerationnetAddress = ni.getInetAddresses(); netAddress.hasMoreElements(); ) {
InetAddressinetAddress = (InetAddress) netAddress.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddressinstanceofInet4Address) {
returninetAddress.getHostAddress().toString();
}
}
}
} catch (SocketExceptione) {
e.printStackTrace();
}
returnnull;
}
/**
* 获取App名称
*/
publicstaticStringgetAppName(Contextcontext) {
StringappName = null;
try {
PackageManagerpackageManager = context.getPackageManager();
ApplicationInfoinfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
appName = (String) packageManager.getApplicationLabel(info);
} catch (PackageManager.NameNotFoundExceptione) {
e.printStackTrace();
}
returnappName;
}
/**
* 获取App版本号
*/
publicstaticStringgetAppVersion(Contextcontext) {
StringversionName = null;
try {
PackageManagerpackageManager = context.getPackageManager();
PackageInfopackageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundExceptione) {
e.printStackTrace();
}
returnversionName;
}
publicstaticbooleanisAppInstalled(Contextcontext, StringpackageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
returntrue;
} catch (PackageManager.NameNotFoundExceptione) {
returnfalse;
}
}
/**
* 是否需要更新
* return 0 -- 不需要更新、1 -- 需要更新、2 -- 需要强制更新
*/
publicstaticintisNeedUpdate(StringthisVersion, StringnewVersion) {
try {
if (thisVersion.equals(newVersion)) {
return0;
}
inttimes = 100;//倍率
floatcurrTimes;//当前倍数
floatv1 = 0;//当前版本计算值
floatv2 = 0;//服务器版本计算值
currTimes = 0.01F;
String[] ver1 = thisVersion.split("\\.");
for (inti = ver1.length - 1; i >= 0; i--) {
v1 += Integer.parseInt(ver1[i]) * currTimes;
currTimes *= times;
}
currTimes = 0.01F;
String[] ver2 = newVersion.split("\\.");
if (Integer.parseInt(ver1[0]) < Integer.parseInt(ver2[0])) {//有重大更新
return2;
}
for (inti = ver1.length - 1; i >= 0; i--) {
v2 += Integer.parseInt(ver2[i]) * currTimes;
currTimes *= times;
}
if (v2 > v1) {
return1;
} else {
return0;
}
} catch (Exceptione) {
if (!thisVersion.equals(newVersion)) {
return1;
} else {
return0;
}
}
}
/**
* 手机号加*
*/
publicstaticStringgetMobile(Stringmobile) {
if (TextUtils.isEmpty(mobile)) {
return"****";
} else {
if (mobile.length() >= 7) {
returnmobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length());
} else {
returnmobile + "****";
}
}
}
/**
* 本地缓存是否可用
*/
publicstaticbooleanisUrlCacheValid(Contextcontext, Stringurl) {
SimpleDateFormatsdf = newSimpleDateFormat("yyyyMMdd");
if (TextUtils.isEmpty(CacheUtil.getUrlCache(url))) {
returnfalse;
} else {
try {
SharedPreferencesisFirst = context.getSharedPreferences("isFirst", context.MODE_PRIVATE);
StringlastModifyTime = isFirst.getString("LastAddressModifyTime", sdf.format(newDate()));
longcacheModifyTime = CacheUtil.getUrlCacheModifyTime(url);
if (cacheModifyTime > sdf.parse(lastModifyTime).getTime()) {
returntrue;
} else {
returnfalse;
}
} catch (Exceptione) {
returnfalse;
}
}
}
/**
* 弹出键盘
*/
publicstaticvoidshowKeyboard(finalViewv) {
v.post(newRunnable() {
@Override
publicvoidrun() {
InputMethodManagerimm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
}
});
}
/**
* 隐藏键盘
*/
publicstaticvoidhideKeyboard(Viewv) {
InputMethodManagerimm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
/**
* 复制到剪贴板
*/
publicstaticvoidcopyBoard(Contextcontext, Stringstr) {
ClipboardManagerboardManager = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
boardManager.setPrimaryClip(ClipData.newPlainText(null, str));
}
/**
* 判断EditText输入是否错误 -- 判断2个字符
*/
publicstaticvoidisEditError2(Stringtext, EditTexteditText) {
if (!TextUtils.isEmpty(text)) {
if (text.contains("\"")) {
editText.setError("不可包含 \" 号");
}
if (text.contains("\\")) {
editText.setError("不可包含 \\ 号");
}
} else {
editText.setError(null);
}
}
/**
* 判断EditText输入是否错误 -- 判断2个字符 带删除按钮
*/
publicstaticvoidisEditError2WithDel(Stringtext, EditTexteditText, ViewdelBtn) {
if (!TextUtils.isEmpty(text)) {
if (text.contains("\"")) {
editText.setError("不可包含 \" 号");
}
if (text.contains("\\")) {
editText.setError("不可包含 \\ 号");
}
delBtn.setVisibility(View.VISIBLE);
} else {
editText.setError(null);
delBtn.setVisibility(View.GONE);
}
}
/**
* 判断EditText输入是否错误 -- 判断2个字符 带删除按钮、带可输入字数
*/
publicstaticvoidisEditError2WithDelWithNum(Stringtext, EditTexteditText, ViewdelBtn, TextViewtvLimit) {
if (!TextUtils.isEmpty(text)) {
if (text.contains("\"")) {
editText.setError("不可包含 \" 号");
}
if (text.contains("\\")) {
editText.setError("不可包含 \\ 号");
}
delBtn.setVisibility(View.VISIBLE);
tvLimit.setText((140 - text.length()) + "");
} else {
editText.setError(null);
delBtn.setVisibility(View.GONE);
tvLimit.setText(140 + "");
}
}
/**
* 判断EditText输入是否错误 -- 判断3个字符
*/
publicstaticvoidisEditError3(Stringtext, EditTexteditText) {
if (!TextUtils.isEmpty(text)) {
isEditError2(text, editText);
if (text.contains("|")) {
editText.setError("不可包含 | 号");
}
} else {
editText.setError(null);
}
}
/**
* 判断EditText输入是否错误 -- 判断4个字符
*/
publicstaticvoidisEditError4(Stringtext, EditTexteditText) {
if (!TextUtils.isEmpty(text)) {
isEditError3(text, editText);
if (text.contains("_")) {
editText.setError("不可包含 _ 号");
}
} else {
editText.setError(null);
}
}
/**
* 数字转换为二位小数
*/
publicstaticStringformatDecimal(doubled, intpointNum) {
StringBufferformat = newStringBuffer("0");
for (inti = 0; i < pointNum; i++) {
if (i == 0) {
format.append(".");
}
format.append("0");
}
returnnewDecimalFormat(format.toString()).format(d);
}
/**
* 格式化当前时间
*/
publicstaticStringformatDate() {
SimpleDateFormatsdf24H = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault());
returnsdf24H.format(newDate());
}
/**
* 获取尾数
*/
publicstaticStringgetEndNum(Strings, intendNum) {
if (!TextUtils.isEmpty(s) && s.length() > endNum) {
returns.substring(s.length() - endNum);
}
returns;
}
/**
* 处理金额 超过1万用简写
*/
publicstaticStringhandleBalance(Stringbalance, intpointNum) {
doubled = Double.parseDouble(balance);
if (d <= 10000) {
returnformatDecimal(d, pointNum);
} else {
returnformatDecimal(d / 10000, pointNum) + "万";
}
}
publicstaticStringhandleBalance2(Stringbalance, intpointNum) {
doubled = Double.parseDouble(balance);
if (d < 10000) {
returnformatDecimal(d, pointNum);
} else {
returnformatDecimal(d / 10000, pointNum) + "万";
}
}
/**
* 处理日期
*/
publicstaticStringhandleTime(Stringtime) {
if (!TextUtils.isEmpty(time) && time.endsWith(".000")) {
returntime.substring(0, time.lastIndexOf("."));
}
returntime;
}
/**
* 拼接收货地址
*/
/*public static String stitchAddress(AddressEntity.DataEntity d){
String address = d.getProvince() + "_" + d.getCity() + "_" + d.getCounty() + "_" + d.getAddress();
return d.getRealName() + "|" + d.getMobile() + "|" + d.getZipCode() + "|" + address;
}*/
/* public static String getLocalMobile(Context context) {
UserInfoEntity.DataEntity userInfo = getUserInfo(context);
if (userInfo != null) {
return userInfo.getMobile();
}
return null;
}*/
/* *//**
* 获取本地的用户信息
*//*
public static UserInfoEntity.DataEntity getUserInfo(Context context) {
SharedPreferences userInfo = context.getSharedPreferences("userInfo", context.MODE_PRIVATE);
String info = userInfo.getString("UserInfo", null);
if (info != null) {
try {
info = DesUtil.decrypt(info, DesUtil.LOCAL_KEY);
UserInfoEntity infoEntity = new Gson().fromJson(info, UserInfoEntity.class);
return infoEntity.getData();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return null;
}*/
/**
* 转换为字符串日期
*/
publicstaticStringgetDateOfNum(intyear, intmonth, intday) {
DecimalFormatformat2 = newDecimalFormat("00");
returnyear + "-" + format2.format(month) + "-" + format2.format(day);
}
}