Android中常用的函数
来源:互联网 发布:制作小游戏软件 编辑:程序博客网 时间:2024/04/30 15:50
//安装apk文件
private void installAPK(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(data, type);
startActivity(intent);
}
//卸载apk文件
private void uninstallAPK(String packageName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("package:" + packageName);
intent.setData(data);
startActivity(intent);
}
//编辑图片大小,保持图片不变形。
public static Bitmap resetImage(Bitmap sourceBitmap,int resetWidth,int resetHeight){
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
int tmpWidth;
int tmpHeight;
float scaleWidth = (float)resetWidth / (float)width;
float scaleHeight = (float)resetHeight / (float)height;
float maxTmpScale = scaleWidth >= scaleHeight ? scaleWidth : scaleHeight;
//保持不变形
tmpWidth = (int)(maxTmpScale * width);
tmpHeight = (int)(maxTmpScale * height);
Matrix m = new Matrix();
m.setScale(maxTmpScale, maxTmpScale, tmpWidth, tmpHeight);
sourceBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, false);
//切图
int x = (tmpWidth - resetWidth)/2;
int y = (tmpHeight - resetHeight)/2;
return Bitmap.createBitmap(sourceBitmap, x, y, resetWidth, resetHeight);
}
//获取本地ip地址
public String getLocalIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
//判断是否为wifi网络
//记得要加权限 android.permission.ACCESS_NETWORK_STATE
public static boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
Android验证email地址的函数
static boolean isValidAddress(String address) {
// Note: Some email provider may violate the standard, so here we only check that
// address consists of two part that are separated by '@', and domain part contains
// at least one '.'.
int len = address.length();
int firstAt = address.indexOf('@');
int lastAt = address.lastIndexOf('@');
int firstDot = address.indexOf('.', lastAt + 1);
int lastDot = address.lastIndexOf('.');
return firstAt > 0 && firstAt == lastAt && lastAt + 1 < firstDot
&& firstDot <= lastDot && lastDot < len - 1;
}
private void installAPK(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
String type = "application/vnd.android.package-archive";
intent.setDataAndType(data, type);
startActivity(intent);
}
//卸载apk文件
private void uninstallAPK(String packageName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("package:" + packageName);
intent.setData(data);
startActivity(intent);
}
//编辑图片大小,保持图片不变形。
public static Bitmap resetImage(Bitmap sourceBitmap,int resetWidth,int resetHeight){
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
int tmpWidth;
int tmpHeight;
float scaleWidth = (float)resetWidth / (float)width;
float scaleHeight = (float)resetHeight / (float)height;
float maxTmpScale = scaleWidth >= scaleHeight ? scaleWidth : scaleHeight;
//保持不变形
tmpWidth = (int)(maxTmpScale * width);
tmpHeight = (int)(maxTmpScale * height);
Matrix m = new Matrix();
m.setScale(maxTmpScale, maxTmpScale, tmpWidth, tmpHeight);
sourceBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), m, false);
//切图
int x = (tmpWidth - resetWidth)/2;
int y = (tmpHeight - resetHeight)/2;
return Bitmap.createBitmap(sourceBitmap, x, y, resetWidth, resetHeight);
}
//获取本地ip地址
public String getLocalIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while (enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
//判断是否为wifi网络
//记得要加权限 android.permission.ACCESS_NETWORK_STATE
public static boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
Android验证email地址的函数
static boolean isValidAddress(String address) {
// Note: Some email provider may violate the standard, so here we only check that
// address consists of two part that are separated by '@', and domain part contains
// at least one '.'.
int len = address.length();
int firstAt = address.indexOf('@');
int lastAt = address.lastIndexOf('@');
int firstDot = address.indexOf('.', lastAt + 1);
int lastDot = address.lastIndexOf('.');
return firstAt > 0 && firstAt == lastAt && lastAt + 1 < firstDot
&& firstDot <= lastDot && lastDot < len - 1;
}
0 0
- Android中常用的函数
- android中常用的函数
- Android中常用的函数
- Android 编程中几个 常用的字符串转数字函数
- android中关于SD卡的常用操作函数
- MYSQL中常用的函数
- server中常用的函数
- VF中常用的函数
- Oracle中常用的函数
- 数据结构中常用的函数
- ClistCtrl中常用的函数
- oracle中常用的函数
- Makefile中常用的函数
- Makefile中常用的函数
- Makefile中常用的函数
- Makefile中常用的函数
- Makefile中常用的函数
- Makefile中常用的函数
- Activity有四种加载模式:
- 美白宫发表美日联合声明 称钓鱼岛系日本施政区域
- java cookie encodeBase64加密
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
- 支持向量机介绍
- Android中常用的函数
- Ad Rotator 组件参考
- 用shift左移位置参数的时候, 每移动一次,$#的值也会产生相应变化
- 移动硬盘中的文件损坏了怎么恢复才好
- Ubuntu密码忘记了解决办法
- hadoop尝试
- ubuntu中sudo apt-get所得的文件
- Crontab的格式
- 没有为 COM 互操作注册程序集 请使用 regasm.exe /tlb 注册该程序集——解决办法