Files
expert-app/app/src/main/java/cn/shangyu/gdxzExpert/utils/UIUtils.java
T
2026-11-03 11:32:42 +08:00

295 lines
9.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 android.app.Activity;
import android.content.Intent;
import cn.shangyu.gdxzExpert.activity.LoginNewActivity;
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 isAccountLogined() {
boolean isLogin = SharePrefUtil.getBoolean(getContext(), Constant.IS_LOGIN, false);
String uuid = SharePrefUtil.getString(getContext(), Constant.uuid, "");
return isLogin && !StringUtil.isEmpty(uuid);
}
public static boolean isLogined() {
return isAccountLogined() && SharePrefUtil.getInt(getContext(), "state", 0) == 6;
}
public static boolean checkAccountLoginAndJump(Context context) {
if (isAccountLogined()) {
return true;
}
long now = System.currentTimeMillis();
if (now - lastJumpLoginTime < 800) {
return false;
}
lastJumpLoginTime = now;
Context targetContext = context != null ? context : getContext();
Intent intent;
if (isAccountLogined()) {
intent = new Intent(targetContext, cn.shangyu.gdxzExpert.activity.LoginSetInfoActivity.class);
intent.putExtra("state", SharePrefUtil.getInt(getContext(), "state", 0));
intent.putExtra("checkInfo", SharePrefUtil.getString(getContext(), "checkInfo", ""));
intent.putExtra("phone", SharePrefUtil.getString(getContext(), Constant.mobile, ""));
} else {
intent = new Intent(targetContext, LoginNewActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (!(targetContext instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
targetContext.startActivity(intent);
return false;
}
private static boolean unusedOldIsLogined() {
// TODO Auto-generated method stub
boolean isLogin = SharePrefUtil.getBoolean(getContext(), Constant.IS_LOGIN, false);
String uuid = SharePrefUtil.getString(getContext(), Constant.uuid, "");
return isLogin && !StringUtil.isEmpty(uuid);
}
/**
* 校验是否已登录,未登录则拉起登录页
*
* @param context 上下文
* @return true 已登录;false 未登录(已跳转登录页)
*/
private static long lastJumpLoginTime = 0L;
public static boolean checkLoginAndJump(Context context) {
if (isLogined()) {
return true;
}
long now = System.currentTimeMillis();
if (now - lastJumpLoginTime < 800) {
return false;
}
lastJumpLoginTime = now;
Context targetContext = context != null ? context : getContext();
Intent intent;
if (isAccountLogined()) {
intent = new Intent(targetContext, cn.shangyu.gdxzExpert.activity.LoginSetInfoActivity.class);
intent.putExtra("state", SharePrefUtil.getInt(getContext(), "state", 0));
intent.putExtra("checkInfo", SharePrefUtil.getString(getContext(), "checkInfo", ""));
intent.putExtra("phone", SharePrefUtil.getString(getContext(), Constant.mobile, ""));
} else {
intent = new Intent(targetContext, LoginNewActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (!(targetContext instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
targetContext.startActivity(intent);
return 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;
// }
}