package me.weyye.todaynews.utils; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.graphics.Bitmap; import android.media.AudioManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.text.TextUtils; import android.util.Base64; import android.view.WindowManager; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * 联网,格式化时间工具类 **/ public class CommonUtil { /** * MD5加密 * * @param paramString * @return */ public static String md5(String paramString) { String returnStr; try { MessageDigest localMessageDigest = MessageDigest.getInstance("MD5"); localMessageDigest.update(paramString.getBytes()); returnStr = byteToHexString(localMessageDigest.digest()); return returnStr; } catch (Exception e) { return paramString; } } public static void callPhone(Context context, final String phone) { Intent phoneIntent = new Intent(); phoneIntent.setAction(Intent.ACTION_CALL); phoneIntent.setData(Uri.parse("tel:" + phone)); context.startActivity(phoneIntent); } /** * 将指定byte数组转换成16进制字符串 * * @param b * @return */ @SuppressLint("DefaultLocale") public static String byteToHexString(byte[] b) { StringBuffer hexString = new StringBuffer(); for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } hexString.append(hex.toUpperCase()); } return hexString.toString(); } /** * 判断当前网络是否可用 * * @param context * @return */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) { return false; } else { NetworkInfo info = connectivity.getActiveNetworkInfo(); return (info != null && info.isAvailable()); } } /** * 获取现在时间 * * @return 返回短时间字符串格式yyyy-MM-dd HH:mm:ss */ @SuppressLint("SimpleDateFormat") public static String getStringDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); return dateString; } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(float dpValue) { final float scale = Resources.getSystem().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * 将px值转换为sp值,保证文字大小不变 * * @param pxValue (DisplayMetrics类中属性scaledDensity) * @return */ public static int px2sp(Context context, float pxValue) { final float fontScale = context.getResources().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } /** * 将sp值转换为px值,保证文字大小不变 * * @param spValue (DisplayMetrics类中属性scaledDensity) * @return */ public static int sp2px(float spValue) { final float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * 根据屏幕宽度确定图片显示的高度 * * @param screenWidth * @param bitmap * @return */ public static int getScreenPicHeight(int screenWidth, Bitmap bitmap) { int picWidth = bitmap.getWidth(); int picHeight = bitmap.getHeight(); int picScreenHeight = 0; picScreenHeight = (screenWidth * picHeight) / picWidth; return picScreenHeight; } /** * 读取流中的数据 * * @param is * @return * @throws Exception */ public static String readFromStream(InputStream is) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } is.close(); String result = bos.toString(); bos.close(); return result; } /** * 将Bitmap转换成Base64字符串 * * @param bmp * @return */ public static String bitmap2StrByBase64(Bitmap bmp) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream); return Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT); } /** * 将String转换成Base64字符串 * * @return */ public static String encodeBase64(String str) { return Base64.encodeToString(str.getBytes(), Base64.DEFAULT); } /** * 获取sha1值 * * @param val * @return * @throws NoSuchAlgorithmException * @throws UnsupportedEncodingException */ public static byte[] sha1(String val) throws NoSuchAlgorithmException, UnsupportedEncodingException { byte[] data = val.getBytes("utf-8"); MessageDigest mDigest = MessageDigest.getInstance("sha1"); return mDigest.digest(data); } /** * 判断某个服务是否正在运行的方法 * * @param mContext * @param serviceName 是包名+服务的类名(例如:net.loonggg.testbackstage.TestService) * @return true代表正在运行,false代表服务没有正在运行 */ public static boolean isServiceWork(Context mContext, String serviceName) { boolean isWork = false; ActivityManager myAM = (ActivityManager) mContext .getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40); if (myList.size() <= 0) { return false; } for (int i = 0; i < myList.size(); i++) { String mName = myList.get(i).service.getClassName().toString(); if (mName.equals(serviceName)) { isWork = true; break; } } return isWork; } /** * 判读手机网络是否是wifi * * @param context * @return */ public static boolean isWifi(Context context) { ConnectivityManager connectMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = connectMgr.getActiveNetworkInfo(); return info != null && info.getType() == ConnectivityManager.TYPE_WIFI; } /* * 获得应用版本名称 */ public static String getVersionName(Context context) { PackageManager pm = context.getPackageManager(); try { // 获得清单文件的信息 PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0); return info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return ""; } } /** * 获取最大音量 * * @param context * @return */ public static int getMaxVolume(Context context) { return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)) .getStreamMaxVolume(AudioManager.STREAM_MUSIC); } /** * 获取当前音量 * * @param context * @return */ public static int getCurVolume(Context context) { return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)) .getStreamVolume(AudioManager.STREAM_MUSIC); } /** * 设置当前音量 * * @param context * @param index */ public static void setCurVolume(Context context, int index) { ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)) .setStreamVolume(AudioManager.STREAM_MUSIC, index, 0); } /** * 手机号验证 * * @param phone * @return */ public static boolean checkPhone(String phone, boolean toast) { if (TextUtils.isEmpty(phone)) { if (toast) ToastUtils.showToast("手机号为空"); return false; } if (phone.length() != 11 || !phone .matches("^((13)|(14)|(15)|(17)|(18))\\d{9}$")) { if (toast) ToastUtils.showToast("手机号格式不对"); return false; } return true; } /** * 手机号验证 * * @param phone * @return */ public static boolean checkPhone(String phone) { return checkPhone(phone, false); } /** * 设置屏幕的背景透明度 * * @param bgAlpha 0.0-1.0 */ public static void setBackgroundAlpha(Activity activity, float bgAlpha) { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); lp.alpha = bgAlpha; //0.0-1.0 activity.getWindow().setAttributes(lp); } }