-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPhoneUtil.java
More file actions
462 lines (426 loc) · 14.6 KB
/
PhoneUtil.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
package com.realmo.utils;
import android.app.ActivityManager;
import android.app.Application;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
/**
* 获取手机相关信息
*/
public class PhoneUtil {
/**
* 获取当前应用程序的包名
* @param context 上下文对象
* @return 返回包名
*/
public static String getAppProcessName(Context context) {
//当前应用pid
int pid = android.os.Process.myPid();
//任务管理类
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//遍历所有应用
List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : infos) {
if (info.pid == pid)//得到当前应用
return info.processName;//返回包名
}
return "";
}
/**
* 获取手机屏幕宽度
*/
public static int getPhoneWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取手机屏幕高度
*/
public static int getPhoneHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 获取控件高度
*/
public static int getWidgetHeight(View v) {
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
return v.getMeasuredHeight();
}
/**
* 获取控件宽度
*/
public static int getWidgetWidth(View v) {
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(w, h);
return v.getMeasuredWidth();
}
/**
* dp 转 px
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px 转 dp
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 获取手机ip
*/
public static String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface ni = (NetworkInterface) en.nextElement();
for (Enumeration netAddress = ni.getInetAddresses(); netAddress.hasMoreElements(); ) {
InetAddress inetAddress = (InetAddress) netAddress.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取App名称
*/
public static String getAppName(Context context) {
String appName = null;
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(context.getPackageName(), 0);
appName = (String) packageManager.getApplicationLabel(info);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return appName;
}
/**
* 获取App版本号
*/
public static String getAppVersion(Context context) {
String versionName = null;
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
versionName = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
/**
* 是否需要更新
* return 0 -- 不需要更新、1 -- 需要更新、2 -- 需要强制更新
*/
public static int isNeedUpdate(String thisVersion, String newVersion) {
try {
if (thisVersion.equals(newVersion)) {
return 0;
}
int times = 100;//倍率
float currTimes;//当前倍数
float v1 = 0;//当前版本计算值
float v2 = 0;//服务器版本计算值
currTimes = 0.01F;
String[] ver1 = thisVersion.split("\\.");
for (int i = 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])) {//有重大更新
return 2;
}
for (int i = ver1.length - 1; i >= 0; i--) {
v2 += Integer.parseInt(ver2[i]) * currTimes;
currTimes *= times;
}
if (v2 > v1) {
return 1;
} else {
return 0;
}
} catch (Exception e) {
if (!thisVersion.equals(newVersion)) {
return 1;
} else {
return 0;
}
}
}
/**
* 手机号加*
*/
public static String getMobile(String mobile) {
if (TextUtils.isEmpty(mobile)) {
return "****";
} else {
if (mobile.length() >= 7) {
return mobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length());
} else {
return mobile + "****";
}
}
}
/**
* 本地缓存是否可用
*/
public static boolean isUrlCacheValid(Context context, String url) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
if (TextUtils.isEmpty(CacheUtil.getUrlCache(url))) {
return false;
} else {
try {
SharedPreferences isFirst = context.getSharedPreferences("isFirst", context.MODE_PRIVATE);
String lastModifyTime = isFirst.getString("LastAddressModifyTime", sdf.format(new Date()));
long cacheModifyTime = CacheUtil.getUrlCacheModifyTime(url);
if (cacheModifyTime > sdf.parse(lastModifyTime).getTime()) {
return true;
} else {
return false;
}
} catch (Exception e) {
return false;
}
}
}
/**
* 弹出键盘
*/
public static void showKeyboard(final View v) {
v.post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
}
});
}
/**
* 隐藏键盘
*/
public static void hideKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
}
/**
* 复制到剪贴板
*/
public static void copyBoard(Context context, String str) {
ClipboardManager boardManager = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
boardManager.setPrimaryClip(ClipData.newPlainText(null, str));
}
/**
* 判断EditText输入是否错误 -- 判断2个字符
*/
public static void isEditError2(String text, EditText editText) {
if (!TextUtils.isEmpty(text)) {
if (text.contains("\"")) {
editText.setError("不可包含 \" 号");
}
if (text.contains("\\")) {
editText.setError("不可包含 \\ 号");
}
} else {
editText.setError(null);
}
}
/**
* 判断EditText输入是否错误 -- 判断2个字符 带删除按钮
*/
public static void isEditError2WithDel(String text, EditText editText, View delBtn) {
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个字符 带删除按钮、带可输入字数
*/
public static void isEditError2WithDelWithNum(String text, EditText editText, View delBtn, TextView tvLimit) {
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个字符
*/
public static void isEditError3(String text, EditText editText) {
if (!TextUtils.isEmpty(text)) {
isEditError2(text, editText);
if (text.contains("|")) {
editText.setError("不可包含 | 号");
}
} else {
editText.setError(null);
}
}
/**
* 判断EditText输入是否错误 -- 判断4个字符
*/
public static void isEditError4(String text, EditText editText) {
if (!TextUtils.isEmpty(text)) {
isEditError3(text, editText);
if (text.contains("_")) {
editText.setError("不可包含 _ 号");
}
} else {
editText.setError(null);
}
}
/**
* 数字转换为二位小数
*/
public static String formatDecimal(double d, int pointNum) {
StringBuffer format = new StringBuffer("0");
for (int i = 0; i < pointNum; i++) {
if (i == 0) {
format.append(".");
}
format.append("0");
}
return new DecimalFormat(format.toString()).format(d);
}
/**
* 格式化当前时间
*/
public static String formatDate() {
SimpleDateFormat sdf24H = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.getDefault());
return sdf24H.format(new Date());
}
/**
* 获取尾数
*/
public static String getEndNum(String s, int endNum) {
if (!TextUtils.isEmpty(s) && s.length() > endNum) {
return s.substring(s.length() - endNum);
}
return s;
}
/**
* 处理金额 超过1万用简写
*/
public static String handleBalance(String balance, int pointNum) {
double d = Double.parseDouble(balance);
if (d <= 10000) {
return formatDecimal(d, pointNum);
} else {
return formatDecimal(d / 10000, pointNum) + "万";
}
}
public static String handleBalance2(String balance, int pointNum) {
double d = Double.parseDouble(balance);
if (d < 10000) {
return formatDecimal(d, pointNum);
} else {
return formatDecimal(d / 10000, pointNum) + "万";
}
}
/**
* 处理日期
*/
public static String handleTime(String time) {
if (!TextUtils.isEmpty(time) && time.endsWith(".000")) {
return time.substring(0, time.lastIndexOf("."));
}
return time;
}
/**
* 拼接收货地址
*/
/*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;
}*/
/**
* 转换为字符串日期
*/
public static String getDateOfNum(int year, int month, int day) {
DecimalFormat format2 = new DecimalFormat("00");
return year + "-" + format2.format(month) + "-" + format2.format(day);
}
}