import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.chansh.csm.R;
public class Utils {
@SuppressLint("SimpleDateFormat")
public static String get_Date() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
return formatter.format(curDate);
}
/**
* 得到设备屏幕的宽度
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
/**
* 得到设备屏幕的高度
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
*
* @param pxValue
* @param fontScale
* (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int px2sp(Context context, int textsize) {
float RATIO, OFFSET_LEFT, OFFSET_TOP;
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(displayMetrics);
// 2.计算与你开发时设定的屏幕大小的纵横比(这里假设你开发时定的屏幕大小是480*800)
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
float ratioWidth = (float) screenWidth / 480;
float ratioHeight = (float) screenHeight / 800;
RATIO = Math.min(ratioWidth, ratioHeight);
if (ratioWidth != ratioHeight) {
if (RATIO == ratioWidth) {
OFFSET_LEFT = 0;
OFFSET_TOP = Math.round((screenHeight - 800 * RATIO) / 2);
} else {
OFFSET_LEFT = Math.round((screenWidth - 480 * RATIO) / 2);
OFFSET_TOP = 0;
}
}
return Math.round(textsize * RATIO);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 适用于header布局是FrameLayout且 header的父布局是LinearLayout的情况
*
* @Title: changeHight
* @Description: TODO(根据版本动态调整title高度)
* @param @param context
* @param @param id 设定文件
* @return void 返回类型
* @throws
*/
// public static void changeHight(Context context, int id) {
// FrameLayout ll = (FrameLayout) ((Activity) context).findViewById(id);
// if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, dip2px(context, 72));
// ll.setPadding(0, dip2px(context, 20), 0, 0);
// ll.setLayoutParams(lp);
// }
// }
/**
* 适用于header布局是FrameLayout且header的父布局是RelativeLayout的情况
*
* @param context
* @param id
*/
// public static void changeHight2(Context context, int id) {
// FrameLayout ll = (FrameLayout) ((Activity) context).findViewById(id);
// if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, dip2px(context, 72));
// ll.setPadding(0, dip2px(context, 20), 0, 0);
// ll.setLayoutParams(lp);
// }
// }
/**
* 适用于header布局是LinearLayout且header的父布局是RelativeLayout的情况
*
* @param context
* @param id
*/
// public static void changeHight3(Context context, int id) {
// LinearLayout ll = (LinearLayout) ((Activity) context).findViewById(id);
// if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, dip2px(context, 72));
// ll.setPadding(0, dip2px(context, 20), 0, 0);
// ll.setLayoutParams(lp);
// }
// }
/**
* 显示toast提醒短时间
*
* @Title: ToastShort
* @return void 返回类型
* @author lianghao
* @throws
*/
public static void ToastShort(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* 显示toast提醒长时间
*
* @Title: ToastShort
* @return void 返回类型
* @throws
*/
public static void ToastLong(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
public static void MyToast(Context context, String message) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.toast, null);
TextView txt = (TextView) view.findViewById(R.id.toast_message);
txt.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
}
/**
* 时间戳转换成时间
*
* @Title: StringToTime
* @return String 返回类型
* @throws
*/
@SuppressLint("SimpleDateFormat")
public static String StringToTime(String time) {
if (time == null || time.equals("") || time.equals("null")) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(new Date(Long.parseLong(time + "000")));
}
/**
* 转换精确到时分
*
* @param time
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String StringToTimeHM(String time) {
if (time == null || time.equals("") || time.equals("null")) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.format(new Date(Long.parseLong(time + "000")));
}
/**
* 忽略年精确到时分
*
* @param time
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String StringToTimeMD(String time) {
if (time == null || time.equals("") || time.equals("null")) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
return sdf.format(new Date(Long.parseLong(time + "000")));
}
/**
*
* @Title: StringToInt
* @return int 返回类型
* @throws
*/
public static int StringToInt(String str) {
if (str == null || str.equals("")) {
return 0;
}
return Integer.parseInt(str);
}
public static Double StringToDouble(String str) {
return Double.parseDouble(str);
}
/**
* 手机号中间四位转为*
*
* @Title: PhoneToX
* @return String 返回类型
* @throws
*/
public static String PhoneToX(String str) {
return str.substring(0, 3) + "****" + str.substring(7, str.length());
}
public static String toDouble(double str) {
java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
return df.format(str);
}
/**
* 提供精确的加法运算。
*
* @param v1
* 被加数
* @param v2
* 加数
* @return 两个参数的和
*/
public static double add(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
* 提供精确的减法运算。
*
* @param v1
* 被减数
* @param v2
* 减数
* @return 两个参数的差
*/
public static double sub(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
/**
* 判断当前应用是否在前端执行
*
* @Title: isApplicationBroughtToBackground
* @return boolean 返回类型 true-后端 false-前端
* @throws
*/
public static boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
public static boolean isAppLicationToRunning(Context context) {
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.chansh.csm";
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME)
|| info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
break;
}
}
return isAppRunning;
}
}