This commit is contained in:
2025-11-18 15:12:24 +08:00
parent c173c425ce
commit a7d2035e70
1361 changed files with 367452 additions and 0 deletions
@@ -0,0 +1,217 @@
package cn.shangyu.gdxzExpert.utils;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;
import java.util.List;
import cn.shangyu.gdxzExpert.application.BaseApplication;
/**
*
* UIUtils.java
* @author gaofei
* @date 2015-8-31 上午9:54:49
* Copyright (C) 2015 河南商宇科技有限公司
*
* 功能说明:一些基本的工具类,1、获取上下文 2、得到主线程id以及主线程 3、得到handler 4、获取资源文件
* 6、把资源id转化为string,7、把资源id转化为drawable 8、把资源id转化为stringArray 9、dip--->px 10、px---->dp
* 11、java代码区设置颜色选择器的方法 12、资源id获得颜色 14、检测网络是否可用 15 、检测Sdcard是否存在
*
* 等等。
*/
public class UIUtils {
private static Toast mToast;
//得到上下文
public static Context getContext(){
return BaseApplication.getContext();
}
//得到主线程id
public static int getMainThreadId(){
return BaseApplication.getMainThreadId();
}
//得到主线程
public static Thread getMainThread(){
return BaseApplication.getMainThread();
}
//得到handler
public static Handler getHandler(){
return BaseApplication.getHandler();
}
//把资源id转化为string
public static String getString(int id){
return getContext().getString(id);
}
//把资源id转化为drawable
public static Drawable getDrawable(int id){
return getContext().getResources().getDrawable(id);
}
//把资源id转化为stringArray
public static String[] getStringArray(int id){
return getContext().getResources().getStringArray(id);
}
//dip--->px 1dp = 1px 1dp = 2px
public static int dip2px(int dip){
//dp和px的转换关系比例值
float density = getContext().getResources().getDisplayMetrics().density;
return (int)(dip*density+0.5);
}
//px---->dp
public static int px2dip(int px){
//dp和px的转换关系比例值
float density = getContext().getResources().getDisplayMetrics().density;
return (int)(px/density+0.5);
}
//判断是否是主线的方法
public static boolean isRunInMainThread(){
return getMainThreadId() == android.os.Process.myTid();
}
/**
* 在主线程执行runnable
*/
public static boolean post(Runnable runnable) {
return getHandler().post(runnable);
}
//保证当前的UI操作在主线程里面运行
public static void runInMainThread(Runnable runnable){
if(isRunInMainThread()){
//如果现在就是在珠现场中,就直接运行run方法
runnable.run();
}else{
//否则将其传递到主线程中运行
post(runnable);
}
}
//java代码区设置颜色选择器的方法
public static ColorStateList getColorStateList(int mTabTextColorResId) {
return getContext().getResources().getColorStateList(mTabTextColorResId);
}
/**
* 把xml文件转化为view
*
*/
public static View inflate(int id){
return View.inflate(getContext(), id, null);
}
public static int getDimens(int id) {
//根据dimens中提供的id,将其对应的dp值转换成相应的像素值大小
return UIUtils.getContext().getResources().getDimensionPixelSize(id);
}
public static void postDelayed(Runnable runnable,long delayTime) {
getHandler().postDelayed(runnable, delayTime);
}
public static void removeCallback(Runnable runnable) {
//移除在当前handler中维护的任务(传递进来的任务)
getHandler().removeCallbacks(runnable);
}
/**
* 资源id获得颜色
*
*/
public static int getColor(int id) {
return getContext().getResources().getColor(id);
}
/**
*
* @author gaofei
* @date 2015-9-6 下午7:41:31
*
* 功能说明:检测网络是否可用
*/
public static boolean isNetWorkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
/**
*
* @author gaofei
* @date 2015-9-6 下午7:41:31
*
* 功能说明:检测Sdcard是否存在
*/
public static boolean isExitsSdcard() {
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
return true;
else
return false;
}
/**
*
* isLogined
* 判断是否登录,以后要加上容联的,以及自动登录需要考虑,现在只是简单读取sp文件
* true表示已登录
* @return
* boolean
*/
public static boolean isLogined() {
// TODO Auto-generated method stub
return SharePrefUtil.getBoolean(getContext(), Constant.IS_LOGIN, false);
}
public static boolean isBackground() {
ActivityManager activityManager = (ActivityManager) getContext()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals( getContext() .getPackageName())) {
/*
BACKGROUND=400 EMPTY=500 FOREGROUND=100
GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
*/
// LogUtil.i( getContext() .getPackageName(), "appimportace ="
// + appProcess.importance
// + ",context.getClass().getName()="
// + getContext() .getClass().getName());
if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
// LogUtil.i( getContext() .getPackageName(), "处于后台"
// + appProcess.processName);
return true;
} else {
// LogUtil.i( getContext() .getPackageName(), "处于前台"
// + appProcess.processName);
return false;
}
}
}
return false;
}
// public static Boolean isActivityBackground()
// {
// ActivityManager manager = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
// ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
// String shortClassName = info.topActivity.getShortClassName(); // 类名
// String className = info.topActivity.getClassName(); // 完整类名
// String packageName = info.topActivity.getPackageName(); // 包
// return true;
// }
}