01 | //安装apk文件 |
02 |
03 | private void installAPK(File file) { |
04 | Intent intent = new Intent(Intent.ACTION_VIEW); |
05 | Uri data = Uri.fromFile(file); |
06 | String type = "application/vnd.android.package-archive" ; |
07 | intent.setDataAndType(data, type); |
08 | startActivity(intent); |
09 | } |
10 |
11 | |
12 |
13 | //卸载apk文件 |
14 |
15 | private void uninstallAPK(String packageName) { |
16 | Intent intent = new Intent(Intent.ACTION_VIEW); |
17 | Uri data = Uri.parse( "package:" + packageName); |
18 | intent.setData(data); |
19 | startActivity(intent); |
20 | } |
21 |
22 |
23 | //编辑图片大小,保持图片不变形。 |
24 | public static Bitmap resetImage(Bitmap sourceBitmap, int resetWidth, int resetHeight){ |
25 | int width = sourceBitmap.getWidth(); |
26 | int height = sourceBitmap.getHeight(); |
27 | int tmpWidth; |
28 | int tmpHeight; |
29 | float scaleWidth = ( float )resetWidth / ( float )width; |
30 | float scaleHeight = ( float )resetHeight / ( float )height; |
31 | float maxTmpScale = scaleWidth >= scaleHeight ? scaleWidth : scaleHeight; |
32 | //保持不变形 |
33 | tmpWidth = ( int )(maxTmpScale * width); |
34 | tmpHeight = ( int )(maxTmpScale * height); |
35 | Matrix m = new Matrix(); |
36 | m.setScale(maxTmpScale, maxTmpScale, tmpWidth, tmpHeight); |
37 | sourceBitmap = Bitmap.createBitmap(sourceBitmap, 0 , 0 , sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, false ); |
38 | //切图 |
39 | int x = (tmpWidth - resetWidth)/ 2 ; |
40 | int y = (tmpHeight - resetHeight)/ 2 ; |
41 | return Bitmap.createBitmap(sourceBitmap, x, y, resetWidth, resetHeight); |
42 | } |
43 |
44 | |
45 |
46 | //获取本地ip地址 |
47 |
48 | public String getLocalIpAddress() { |
49 | try { |
50 | Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); |
51 | while (en.hasMoreElements()) { |
52 | NetworkInterface intf = en.nextElement(); |
53 | Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); |
54 | while (enumIpAddr.hasMoreElements()) { |
55 | InetAddress inetAddress = enumIpAddr.nextElement(); |
56 | if (!inetAddress.isLoopbackAddress()) { |
57 | return inetAddress.getHostAddress().toString(); |
58 | } |
59 | } |
60 | } |
61 | } catch (SocketException ex) { |
62 | ex.printStackTrace(); |
63 | } |
64 | return null ; |
65 | } |
66 |
67 | |
68 |
69 | //判断是否为wifi网络 |
70 |
71 | //记得要加权限 android.permission.ACCESS_NETWORK_STATE |
72 |
73 | public static boolean isWifi(Context mContext) { |
74 | ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
75 | NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); |
76 | if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { |
77 | return true ; |
78 | } |
79 | return false ; |
80 | } |
81 |
82 | Android验证email地址的函数 |
83 |
84 |
85 | |
86 |
87 | static boolean isValidAddress(String address) { |
88 | // Note: Some email provider may violate the standard, so here we only check that |
89 | // address consists of two part that are separated by '@', and domain part contains |
90 | // at least one '.'. |
91 | int len = address.length(); |
92 | int firstAt = address.indexOf( '@' ); |
93 | int lastAt = address.lastIndexOf( '@' ); |
94 | int firstDot = address.indexOf( '.' , lastAt + 1 ); |
95 | int lastDot = address.lastIndexOf( '.' ); |
96 | return firstAt > 0 && firstAt == lastAt && lastAt + 1 < firstDot |
97 | && firstDot <= lastDot && lastDot < len - 1 ; |
98 | } |