工具类
2020-08-13 本文已影响0人
我家猫猫不睡觉
LogUtil
public class LogUtil {
private final static String TAG = "LogUtil";
private static final String TAG_TRACE = "MyLogTrace";
private static final class StackTraceDebug extends RuntimeException {
final static private long serialVersionUID = 27058374L;
}
private static boolean DEBUG = true;
private static boolean LOGTRACE = true;
private LogUtil() {
}
public static void trace(Object object) {
if (LOGTRACE) {
StackTraceElement[] traces = Thread.currentThread().getStackTrace();
StackTraceElement trace = traces[3];
Log.d(TAG_TRACE, addThreadInfo(object.getClass().getSimpleName() + " : " + trace.getMethodName()));
}
}
public static boolean isDebug() {
return DEBUG;
}
public static void setDebug(final boolean isDebug) {
DEBUG = isDebug;
}
public static boolean isLogTrace() {
return LOGTRACE;
}
public static void setLogTrace(final boolean isLogTrace) {
LOGTRACE = isLogTrace;
}
private static String addThreadInfo(final String msg) {
final String threadName = Thread.currentThread().getName();
final String shortName = threadName.startsWith("OkHttp") ? "OkHttp" : threadName;
return "[" + shortName + "] " + msg;
}
public static void v(final String msg) {
if (DEBUG) {
Log.v(TAG, addThreadInfo(msg));
}
}
public static void v(final String msg, final Throwable t) {
if (DEBUG) {
Log.v(TAG, addThreadInfo(msg), t);
}
}
public static void d(final String msg) {
if (DEBUG) {
Log.d(TAG, addThreadInfo(msg));
}
}
public static void d(final String msg, final Throwable t) {
if (DEBUG) {
Log.d(TAG, addThreadInfo(msg), t);
}
}
public static void i(final String msg) {
if (DEBUG) {
Log.i(TAG, addThreadInfo(msg));
}
}
public static void i(final String msg, final Throwable t) {
if (DEBUG) {
Log.i(TAG, addThreadInfo(msg), t);
}
}
public static void w(final String TAG, final String msg) {
Log.w(TAG, addThreadInfo(msg));
}
public static void w(final String msg) {
Log.w(TAG, addThreadInfo(msg));
}
public static void w(final String msg, final Throwable t) {
Log.w(TAG, addThreadInfo(msg), t);
}
public static void e(final String msg) {
Log.e(TAG, addThreadInfo(msg));
}
public static void e(String TAG, final String msg) {
Log.e(TAG, addThreadInfo(msg));
}
public static void e(final String msg, final Throwable t) {
Log.e(TAG, addThreadInfo(msg), t);
}
public static void logStackTrace(final String msg) {
try {
throw new StackTraceDebug();
} catch (final StackTraceDebug dbg) {
d(msg, dbg);
}
}
}
删除数值左右多余的0以及结尾是小数点的情况
public class DelZeroUtil {
public static String getNumber(String number) {
String value = BigDecimal.valueOf(Double.parseDouble(number)).stripTrailingZeros().toPlainString();
if (value.endsWith(".")){
value = value.substring(0,value.length() - 1);
}
return value;
}
}
EditText 数值输入区间监听
public class EditInputFilter implements InputFilter {
/**
* 最大数字
*/
private double mMaxValue;
/**
* 小数点后的数字的位数
*/
private int mPointLength;
Pattern p;
public EditInputFilter(double maxValue, int pointLength) {
this.mMaxValue = maxValue;
this.mPointLength = pointLength;
p = Pattern.compile("[0-9]*"); //除数字外的其他的
}
/**
* source 新输入的字符串
* start 新输入的字符串起始下标,一般为0
* end 新输入的字符串终点下标,一般为source长度-1
* dest 输入之前文本框内容
* dstart 原内容起始坐标,一般为0
* dend 原内容终点坐标,一般为dest长度-1
*/
@Override
public CharSequence filter(CharSequence src, int start, int end,
Spanned dest, int dstart, int dend) {
String oldtext = dest.toString();
//验证删除等按键
if ("".equals(src.toString())) {
return null;
}
//验证非数字或者小数点的情况
Matcher m = p.matcher(src);
if (oldtext.contains(".")) {
//已经存在小数点的情况下,只能输入数字
if (!m.matches()) {
return null;
}
} else {
//未输入小数点的情况下,可以输入小数点和数字
if (!m.matches() && !src.equals(".")) {
return null;
}
}
//验证输入金额的大小
if (!src.toString().equals("")) {
String value = oldtext + src.toString();
if (value.startsWith(".")) {
ToastUtil.showToast("金额不能以小数点开头");
return dest.subSequence(dstart, dend);
}
double dold = Double.parseDouble(value);
if (dold > mMaxValue) {
ToastUtil.showToast("最大金额不能大于" + mMaxValue + "元");
return dest.subSequence(dstart, dend);
} else if (dold == mMaxValue) {
if (src.toString().equals(".")) {
ToastUtil.showToast("最大金额不能大于" + mMaxValue + "元");
return dest.subSequence(dstart, dend);
}
}
}
//验证小数位精度是否正确
if (oldtext.contains(".")) {
int index = oldtext.indexOf(".");
int len = dend - index;
//小数位只能2位
if (len > mPointLength) {
CharSequence newText = dest.subSequence(dstart, dend);
return newText;
}
}
return dest.subSequence(dstart, dend) + src.toString();
}
}
随机字符串(验证码)
public class RandomStrUtil {
//随机大写字母和数字
public static String getRandomString(int length) {
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(2);
long result = 0;
switch (number) {
case 0:
result = Math.round(Math.random() * 25 + 65);
sb.append(((char) result));
break;
case 1:
sb.append((new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
//随机大小写字母和数字
public static String getRandomString2(int length) {
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(3);
long result = 0;
switch (number) {
case 0:
result = Math.round(Math.random() * 25 + 65);
sb.append(((char) result));
break;
case 1:
result = Math.round(Math.random() * 25 + 97);
sb.append(((char) result));
break;
case 2:
sb.append((new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
//随机大写或者小写或者数字
public static String getRandomString3(int length, int type) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
long result = 0;
switch (type) {
case 0: //大写
result = Math.round(Math.random() * 25 + 65);
sb.append(String.valueOf((char) result));
break;
case 1: //小写
result = Math.round(Math.random() * 25 + 97);
sb.append(String.valueOf((char) result));
break;
case 2: //数字
sb.append(String.valueOf(new Random().nextInt(10)));
break;
}
}
return sb.toString();
}
}
ScreenUtils
public class ScreenUtils {
/**
* 获取屏幕宽 px
*
* @param context 上下文
* @return 屏幕宽 px
*/
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;// 屏幕宽(像素,如:480px)
}
/**
* 获取屏幕高 px
*
* @param context 上下文
* @return 屏幕高 px
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;// 屏幕高(像素,如:800px)
}
/**
* dp转px
*
* @param context 上下文
* @param dpVal dp
* @return 像素px
*/
public static float dp2px(Context context, float dpVal) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
}
/**
* dp转px
* @param context
* @param dipValue
* @return
*/
public static int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* sp转px
*
* @param context 上下文
* @param spVal sp
* @return 像素px
*/
public static float sp2px(Context context, float spVal) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources().getDisplayMetrics());
}
/**
* px转dp
*
* @param context 上下文
* @param pxVal 像素px
* @return dp
*/
public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px转sp
*
* @param context 上下文
* @param pxVal 像素px
* @return sp
*/
public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
/**
* 计算出来的位置,y方向就在anchorView的上面和下面对齐显示,x方向就是与屏幕右边对齐显示
* 如果anchorView的位置有变化,就可以适当自己额外加入偏移来修正
*
* @param anchorView 呼出window的view
* @param contentView window的内容布局
* @return window显示的左上角的xOff, yOff坐标
*/
public static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
final int windowPos[] = new int[2];
final int anchorLoc[] = new int[2];
// 获取锚点View在屏幕上的左上角坐标位置
anchorView.getLocationOnScreen(anchorLoc);
final int anchorHeight = anchorView.getHeight();
// 获取屏幕的高宽
final int screenHeight = getScreenHeight(anchorView.getContext());
final int screenWidth = getScreenWidth(anchorView.getContext());
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// 计算contentView的高宽
final int windowHeight = contentView.getMeasuredHeight();
final int windowWidth = contentView.getMeasuredWidth();
// 判断需要向上弹出还是向下弹出显示
final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
if (isNeedShowUp) {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] - windowHeight;
} else {
windowPos[0] = screenWidth - windowWidth;
windowPos[1] = anchorLoc[1] + anchorHeight;
}
return windowPos;
}
/**
* 获取屏幕内容高度
* @param activity
* @return
*/
public static int getScreenHeight(Activity activity) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int result = 0;
int resourceId = activity.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = activity.getResources().getDimensionPixelSize(resourceId);
}
int screenHeight = dm.heightPixels - result;
return screenHeight;
}
}
连续点击检查
public abstract class FastClickUtil implements View.OnClickListener {
// 两次点击间隔不能少于1000ms
private static final long FAST_CLICK_DELAY_TIME = 600;
private static long lastClickTime;
/**
* 是否是快速点击
*
* @return true 为快速点击
*/
public static boolean isFastClick() {
boolean flag = true;
long currentClickTime = System.currentTimeMillis();
long intervalTime = currentClickTime - lastClickTime;
LogUtil.e("FastClickUtil", String.valueOf(intervalTime));
if ((currentClickTime - lastClickTime) >= FAST_CLICK_DELAY_TIME ) {
flag = false;
}
lastClickTime = currentClickTime;
return flag;
}
}
@Override
public void onClick(View view) {
if (FastClickUtil.isFastClick()) {
return;
}
switch (view.getId()) {
case R.id.backImg:
AppManager.getAppManager(this).finishActivity(this);
break;
}
}
ToastUtil
public class ToastUtil {
private static Handler handler = new Handler(Looper.getMainLooper());
private volatile static Toast toast = null;
/**
* 根据设置的文本显示
*
* @param msg
*/
public static void showToast(String msg) {
showToast(msg, Toast.LENGTH_LONG);
}
/**
* 根据设置的资源文件显示
*
* @param resId
*/
public static void showToast(final int resId) {
showToast(resId, Toast.LENGTH_LONG);
}
/**
* 显示一个文本并且设置时长
*
* @param msg
* @param len
*/
public static void showToast(final CharSequence msg, final int len) {
if (TextUtils.isEmpty(msg)) return;
handler.post(new Runnable() {
@Override
public void run() {
if (toast != null) {
toast.setText(msg);
toast.setDuration(len);
} else {
toast = Toast.makeText(BaseApplication.getContext(), msg, len);
}
toast.show();
}
});
}
/**
* 资源文件方式显示文本
*
* @param resId
* @param len
*/
public static void showToast(final int resId, final int len) {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (ToastUtil.class) {
if (toast != null) {
toast.setText(resId);
toast.setDuration(len);
} else {
toast = Toast.makeText(BaseApplication.getContext(), resId, len);
}
toast.show();
}
}
});
}
}
权限申请
依赖
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
使用
final RxPermissions rxPermissions = new RxPermissions(this);
getCompositeDisposable().add(rxPermissions.requestEach(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Permission>() {
@Override
public void accept(Permission permission) throws Exception {
if (permission.granted) {
LogUtil.w(TAG, "权限已授予");
} else if (permission.shouldShowRequestPermissionRationale) {
AppManager.getAppManager(getMyContext()).finishActivity(PlatformLoginActivity.this);
} else {
jumpDialog();
}
}
}));
private void jumpDialog() {
if (dialog == null) {
dialog = new SureCancelDialog.Builder(this)
.setContent("您需要开启存储权限,方能继续使用")
.comfirm(new SureCancelDialog.OnConfirmClickListener() {
@Override
public void confirm() {
dialog.dismiss();
isJumpSetting = true;
PermissionsUtil permissionsUtil = new PermissionsUtil(PlatformLoginActivity.this);
permissionsUtil.jumpPermissionPage();
}
})
.cancel(new SureCancelDialog.OnCancelClickListener() {
@Override
public void cancel() {
dialog.dismiss();
AppManager.getAppManager(getMyContext()).AppExit(PlatformLoginActivity.this);
}
}).create();
}
if (dialog != null) {
dialog.show();
}
}
工具类
public class PermissionsUtil {
private final String TAG = "PermissionPageManager";
private Context mContext;
//自己的项目包名
//private String packageName="com.donkor.demo";
public PermissionsUtil(Context context) {
this.mContext = context;
}
public void jumpPermissionPage() {
String name = Build.MANUFACTURER;
LogUtil.e(TAG, "jumpPermissionPage --- name : " + name);
switch (name) {
case "HUAWEI":
goHuaWeiMainager();
break;
case "vivo":
goVivoMainager();
break;
case "OPPO":
goOppoMainager();
break;
case "Coolpad":
goCoolpadMainager();
break;
case "Meizu":
goMeizuMainager();
break;
case "Xiaomi":
goXiaoMiMainager();
break;
case "samsung":
goSangXinMainager();
break;
case "Sony":
goSonyMainager();
break;
case "LG":
goLGMainager();
break;
default:
goIntentSetting();
break;
}
}
private void goLGMainager(){
try {
Intent intent = new Intent(mContext.getPackageName());
ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
intent.setComponent(comp);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
goIntentSetting();
}
}
private void goSonyMainager(){
try {
Intent intent = new Intent(mContext.getPackageName());
ComponentName comp = new ComponentName("com.sonymobile.cta", "com.sonymobile.cta.SomcCTAMainActivity");
intent.setComponent(comp);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
goIntentSetting();
}
}
private void goHuaWeiMainager() {
try {
Intent intent = new Intent(mContext.getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");
intent.setComponent(comp);
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
goIntentSetting();
}
}
private static String getMiuiVersion() {
String propName = "ro.miui.ui.version.name";
String line;
BufferedReader input = null;
try {
Process p = Runtime.getRuntime().exec("getprop " + propName);
input = new BufferedReader(
new InputStreamReader(p.getInputStream()), 1024);
line = input.readLine();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
return null;
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return line;
}
private void goXiaoMiMainager() {
String rom = getMiuiVersion();
Intent intent=new Intent();
if ("V6".equals(rom) || "V7".equals(rom)) {
intent.setAction("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
intent.putExtra("extra_pkgname", mContext.getPackageName());
} else if ("V8".equals(rom) || "V9".equals(rom)) {
intent.setAction("miui.intent.action.APP_PERM_EDITOR");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
intent.putExtra("extra_pkgname", mContext.getPackageName());
} else {
goIntentSetting();
}
mContext.startActivity(intent);
}
private void goMeizuMainager() {
try {
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("context.getPackageName()", mContext.getPackageName());
mContext.startActivity(intent);
} catch (ActivityNotFoundException localActivityNotFoundException) {
localActivityNotFoundException.printStackTrace();
goIntentSetting();
}
}
private void goSangXinMainager() {
//三星4.3可以直接跳转
goIntentSetting();
}
private void goIntentSetting() {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", mContext.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings",
"com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName",
mContext.getPackageName());
}
mContext.startActivity(localIntent);
}
private void goOppoMainager() {
doStartApplicationWithPackageName("com.coloros.safecenter");
}
/**
* doStartApplicationWithPackageName("com.yulong.android.security:remote")
* 和Intent open = getPackageManager().getLaunchIntentForPackage("com.yulong.android.security:remote");
* startActivity(open);
* 本质上没有什么区别,通过Intent open...打开比调用doStartApplicationWithPackageName方法更快,也是android本身提供的方法
*/
private void goCoolpadMainager() {
doStartApplicationWithPackageName("com.yulong.android.security:remote");
/* Intent openQQ = getPackageManager().getLaunchIntentForPackage("com.yulong.android.security:remote");
startActivity(openQQ);*/
}
private void goVivoMainager() {
doStartApplicationWithPackageName("com.bairenkeji.icaller");
/* Intent openQQ = getPackageManager().getLaunchIntentForPackage("com.vivo.securedaemonservice");
startActivity(openQQ);*/
}
private void doStartApplicationWithPackageName(String packageName) {
// 通过包名获取此APP详细信息,包括Activities、services、versioncode、name等等
PackageInfo packageinfo = null;
try {
packageinfo = mContext.getPackageManager().getPackageInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageinfo == null) {
return;
}
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(mContext.getPackageName());
// 通过getPackageManager()的queryIntentActivities方法遍历
List<ResolveInfo> resolveinfoList = mContext.getPackageManager()
.queryIntentActivities(resolveIntent, 0);
ResolveInfo resolveinfo = resolveinfoList.iterator().next();
if (resolveinfo != null) {
// packageName参数2 = 参数 packname
// 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式:packageName参数2.mainActivityname]
String className = resolveinfo.activityInfo.name;
// LAUNCHER Intent
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 设置ComponentName参数1:packageName参数2:MainActivity路径
ComponentName cn = new ComponentName(mContext.getPackageName(), className);
intent.setComponent(cn);
try {
mContext.startActivity(intent);
} catch (Exception e) {
goIntentSetting();
e.printStackTrace();
}
}
}
}