安卓开发过程中的工具类
来源:互联网 发布:telnet测试端口问题 编辑:程序博客网 时间:2024/06/07 13:51
对指定字符串进行MD5加密
public static String EncryptMD5(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; try { byte[] btInput = s.getBytes(); // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(btInput); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { e.printStackTrace(); return null; }}
判断email格式是否正确
public static boolean isEmail(String email) { String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(email); return m.matches();}
判断mobile格式是否正确
public static boolean isMobile(String mobile) { String str = "^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])[0-9]{8}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(mobile); return m.matches();}
验证是否是身份证号
public static boolean isIDCard(String IDCard) { Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])"); Matcher m = idNumPattern.matcher(IDCard); return m.matches();}
根据身份证号码获取生日
public static String getBirthdayFromIdCard(String idCard) { if (TextUtils.isEmpty(idCard)) return null; String date = ""; if (idCard.length() == 18) { date = idCard.substring(6, 14); } else if (idCard.length() == 15) { date = "19" + idCard.substring(6, 12); } else {//身份证号有误或者不是身份证号 return null; } String birthday = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8); return birthday;}
根据身份证号码获取性别,倒数第二位是奇数就是男性,偶数就是女性
public static Integer getSexFromIdCard(String idCard) { if (TextUtils.isEmpty(idCard)) return null; if (idCard.length() < 18) return null; String sex = idCard.substring(16, 17); if (Integer.parseInt(sex) % 2 == 0) {//女 return 0; } else {//男 return 1; }}
根据语言系统判断是否是中国
public static boolean isZh() { Locale locale = BaseApplication.getInstance().getResources().getConfiguration().locale; String language = locale.getLanguage(); if (language.startsWith("zh")) { return true; } else { return false; }}
获取联系人电话
private String getContactPhone(Context context, Cursor cursor) { int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); int phoneNum = cursor.getInt(phoneColumn); String phoneResult = ""; if (phoneNum > 0) { // 获得联系人的ID号 int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); String contactId = cursor.getString(idColumn); // 获得联系人的电话号码的cursor; Cursor phones = context.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); if (phones.moveToFirst()) { // 遍历所有的电话号码 for (; !phones.isAfterLast(); phones.moveToNext()) { int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); int phone_type = phones.getInt(typeindex); String phoneNumber = phones.getString(index); switch (phone_type) { case 2: phoneResult = phoneNumber; break; } } if (!phones.isClosed()) { phones.close(); } } } return phoneResult;}
拨打指定号码的电话
public static void call(Context context, String mobile) { Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.setData(Uri.parse("tel:" + mobile)); context.startActivity(intent);}
隐藏软键盘
public static void hideSoftInput(Activity activity, View view) { if (view == null) return; InputMethodManager imm = (InputMethodManager) activity .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0);}
显示软键盘
public static void showSoftInput(Activity activity, View view) { view.setFocusable(true); view.setFocusableInTouchMode(true); view.requestFocus(); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);}
防止按钮多次点击
private static long lastClickTime;
public static boolean isFastDoubleClick(int number) { long time = System.currentTimeMillis(); long timeD = time - lastClickTime; if (0 < timeD && timeD < number) { return false; } lastClickTime = time; return true;}
获取手机app应用
public static List<OtherAppInfo> scanLocalInstallAppList(PackageManager packageManager) { List<OtherAppInfo> otherAppInfoList = new ArrayList<>(); Map<String, Object> mapAllLocalApp = new HashMap<String, Object>(); try { List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); for (int i = 0; i < installedPackages.size(); i++) { PackageInfo packageInfo = installedPackages.get(i); //是否要过滤掉系统app if ((ApplicationInfo.FLAG_SYSTEM & packageInfo.applicationInfo.flags) != 0) { continue; } OtherAppInfo otherAppInfo = new OtherAppInfo(); if (packageInfo.applicationInfo.packageName.equals("com.lenew.appstore")) { mapAllLocalApp.put(packageInfo.applicationInfo.packageName, packageInfo.versionCode); continue; } otherAppInfo.appName = packageInfo.applicationInfo.loadLabel(packageManager).toString(); otherAppInfo.appPackageName = packageInfo.packageName; otherAppInfo.appImg = packageInfo.applicationInfo.loadIcon(packageManager); otherAppInfo.versionName = packageInfo.versionName; otherAppInfo.versionCode = packageInfo.versionCode; //获取apk的大小 String dir = packageInfo.applicationInfo.publicSourceDir; int size = Integer.valueOf((int) new File(dir).length()); otherAppInfo.appCount = size; // otherAppInfo.packageVersions = packageInfo.versionName; otherAppInfoList.add(otherAppInfo); } } catch (Exception e) { e.printStackTrace(); //获取包应用失败 } return otherAppInfoList;}
格式化单位
public static String getFormatSize(int size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + "B"; } double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "K"; } double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "M"; } double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "G"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "T";}
文件大小转字符串(带单位K/KB/MB/GB)
public static String fileSizeToString(long size) { if (size < 1024) { return size + "B"; } else if (size < 1024 * 1024) { return String.format("%.2fK", (double) size / 1024); } else if (size < 1024 * 1024 * 1024) { return String.format("%.2fM", (double) size / 1024 / 1024); } else if (size < 1024 * 1024 * 1024 * 1024) { return String.format("%.2fG", (double) size / 1024 / 1024 / 1024); } else { return String.format("%.2fT", (double) size / 1024 / 1024 / 1024 / 1024); }}
px值转dip或dp值,保证尺寸大小不变
public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f);}
dip或dp值转换为px值,保证尺寸大小不变
public static int dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f);}
将px值转换为sp值,保证文字大小不变
public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f);}
将sp值转换成px值,保证文字大小不变
public static int sp2px(Context context, float spValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f);}
删除单位文件
public static boolean deleteFile(String sPath) { boolean flag = false; if (sPath == null) { return flag; } File file = new File(sPath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag;}
计算百分比
public static int accuracy(double presentNum, double allCount) { DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(); //可以设置精确几位小数 df.setMaximumFractionDigits(1); //模式 例如四舍五入 df.setRoundingMode(RoundingMode.HALF_UP); double accuracy_num = presentNum / allCount * 100; return (int) accuracy_num; // return df.format(accuracy_num);}
阅读全文
0 0
- 安卓开发过程中的工具类
- 安卓开发中的优秀工具
- 安卓开发工具
- [安卓开发工具]
- 安卓开发-工具类的使用
- 安卓开发常用的工具类
- 安卓开发工具类-SharedPreferences
- 安卓开发工具类-页面跳转
- 安卓开发工具类-MD5加密
- 安卓开发-安卓启动过程
- 安卓开发工具安卓
- 安卓开发工具问题解决方安
- 安卓开发工具类-Toast简单版工具
- 安卓开发工具下载
- 安卓开发工具入门
- 安卓开发工具安装
- 安卓开发工具记录
- 安卓开发工具下载
- android轮询最佳实践service+AlarmManager+Thread
- 网页开发中遇到的小问题
- ubuntu下安装使用MySQL的一些注意事项(远程无法连接解决方法)
- TypeError: resize_images() takes at least 3 arguments (3 given)
- 云帮(ACP)10月升级说明
- 安卓开发过程中的工具类
- 【LizardFS】的安装使用
- 自定义View(三),仿小米运动计步
- JavaScript RegExp 正则表达式基础详谈
- 个人问题
- Keras和TensorFlow的关系和区别
- 第八章 基于项的图形视图
- phpstrom-注册
- eclipse安装反编译插件记录