android中常用的函数

01//安装apk文件
02 
03private 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 
15private 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 
73public 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 
82Android验证email地址的函数
83 
84 
85  
86 
87static 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 '.'.
91int len = address.length();
92int firstAt = address.indexOf('@');
93int lastAt = address.lastIndexOf('@');
94int firstDot = address.indexOf('.', lastAt + 1);
95int lastDot = address.lastIndexOf('.');
96return firstAt > 0 && firstAt == lastAt && lastAt + 1 < firstDot
97&& firstDot <= lastDot && lastDot < len - 1;
98}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值