Skip to content

Commit

Permalink
优化ActivityBase
Browse files Browse the repository at this point in the history
新增Activity控制方法
新增正则匹配方法
新增DB控制类
  • Loading branch information
秋逸 committed Apr 4, 2017
1 parent 28ec95b commit bbc741b
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.vondear.rxtools;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import java.util.List;
import java.util.Stack;


/**
Expand All @@ -16,6 +18,88 @@
*/
public class RxActivityUtils {

private static Stack<Activity> activityStack;

/**
* 添加Activity 到栈
*
* @param activity
*/
public static void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<>();
}
activityStack.add(activity);
}

/**
* 获取当前的Activity(堆栈中最后一个压入的)
*/
public static Activity currentActivity() {
Activity activity = activityStack.lastElement();
return activity;
}

/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public static void finishActivity() {
Activity activity = activityStack.lastElement();

}

/**
* 结束指定的Activity
*
* @param activity
*/
public static void finishActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
}

/**
* 结束指定类名的Activity
*/
public static void finishActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
finishActivity();
}
}
}

/**
* 结束所有的Activity、
*/
public static void finishAllActivity() {
int size = activityStack.size();
for (int i = 0; i < size; i++) {
if (null != activityStack.get(i)) {
activityStack.get(i).finish();
}
}
activityStack.clear();
}

public static void AppExit(Context context) {
try {
finishAllActivity();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.restartPackage(context.getPackageName());
System.exit(0);
} catch (Exception e) {

}
}

public static Stack<Activity> getActivityStack() {
return activityStack;
}

/**
* 判断是否存在指定Activity
*
Expand Down
48 changes: 48 additions & 0 deletions RxTools-library/src/main/java/com/vondear/rxtools/RxDBUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.vondear.rxtools;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Created by vonde on 2017/4/4.
*/

public class RxDBUtils {

/** 读文件buffer.*/
private static final int FILE_BUFFER = 1024;
private static String TAG="RxDBUtils";

/**
* 数据库导出到sdcard.
* @param context
* @param dbName 数据库名字 例如 xx.db
*/
public static void exportDb2Sdcard(Context context, String dbName) {
String filePath = context.getDatabasePath(dbName).getAbsolutePath();
byte[] buffer = new byte[FILE_BUFFER];
int length;
OutputStream output;
InputStream input;
try {
input = new FileInputStream(new File((filePath)));
output = new FileOutputStream(context.getExternalCacheDir() + File.separator + dbName);
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
Log.i(TAG, "mv success!");
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
}
91 changes: 91 additions & 0 deletions RxTools-library/src/main/java/com/vondear/rxtools/RxDataUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -907,4 +909,93 @@ public static String stringFormat(String formatStr, int number) {
public static String stringFormat(String formatStr, float number) {
return String.format(formatStr, number);
}

private static final DecimalFormat amountFormat = new DecimalFormat("###,###,###,##0.00");
/**
* 金额格式化
*
* @param value 数值
* @return
*/
public static String getAmountValue(double value) {
return amountFormat.format(value);
}
/**
* 金额格式化
*
* @param value 数值
* @return
*/
public static String getAmountValue(String value) {
return amountFormat.format(Double.parseDouble(value));
}

/**
* 四舍五入
*
* @param value 数值
* @param digit 保留小数位
* @return
*/
public static String getRoundUp(BigDecimal value, int digit) {
return value.setScale(digit, BigDecimal.ROUND_HALF_UP).toString();
}

/**
* 四舍五入
*
* @param value 数值
* @param digit 保留小数位
* @return
*/
public static String getRoundUp(double value, int digit) {
BigDecimal result = new BigDecimal(value);
return result.setScale(digit, BigDecimal.ROUND_HALF_UP).toString();
}
/**
* 四舍五入
*
* @param value 数值
* @param digit 保留小数位
* @return
*/
public static String getRoundUp(String value, int digit) {
BigDecimal result = new BigDecimal(Double.parseDouble(value));
return result.setScale(digit, BigDecimal.ROUND_HALF_UP).toString();
}

/**
* 获取百分比(乘100)
*
* @param value 数值
* @param digit 保留小数位
* @return
*/
public static String getPercentValue(BigDecimal value, int digit) {
BigDecimal result = value.multiply(new BigDecimal(100));
return getRoundUp(result, digit);
}

/**
* 获取百分比(乘100)
*
* @param value 数值
* @param digit 保留小数位
* @return
*/
public static String getPercentValue(double value, int digit) {
BigDecimal result = new BigDecimal(value);
return getPercentValue(result, digit);
}

/**
* 获取百分比(乘100,保留两位小数)
*
* @param value 数值
* @return
*/
public static String getPercentValue(double value) {
BigDecimal result = new BigDecimal(value);
return getPercentValue(result, 2);
}
}
71 changes: 71 additions & 0 deletions RxTools-library/src/main/java/com/vondear/rxtools/RxRegUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,75 @@ public static boolean isIP(String string) {
public static boolean isMatch(String regex, String string) {
return !isNullString(string) && Pattern.matches(regex, string);
}

/**
* 验证固定电话号码
*
* @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447
* <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字,
* 数字之后是空格分隔的国家(地区)代码。</p>
* <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号——
* 对不使用地区或城市代码的国家(地区),则省略该组件。</p>
* <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p>
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkPhone(String phone) {
String regex = "(\\+\\d+)?(\\d{3,4}\\-?)?\\d{7,8}$";
return Pattern.matches(regex, phone);
}

/**
* 验证整数(正整数和负整数)
*
* @param digit 一位或多位0-9之间的整数
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkDigit(String digit) {
String regex = "\\-?[1-9]\\d+";
return Pattern.matches(regex, digit);
}

/**
* 验证整数和浮点数(正负整数和正负浮点数)
*
* @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkDecimals(String decimals) {
String regex = "\\-?[1-9]\\d+(\\.\\d+)?";
return Pattern.matches(regex, decimals);
}

/**
* 验证空白字符
*
* @param blankSpace 空白字符,包括:空格、\t、\n、\r、\f、\x0B
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkBlankSpace(String blankSpace) {
String regex = "\\s+";
return Pattern.matches(regex, blankSpace);
}

/**
* 验证日期(年月日)
*
* @param birthday 日期,格式:1992-09-03,或1992.09.03
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkBirthday(String birthday) {
String regex = "[1-9]{4}([-./])\\d{1,2}\\1\\d{1,2}";
return Pattern.matches(regex, birthday);
}

/**
* 匹配中国邮政编码
*
* @param postcode 邮政编码
* @return 验证成功返回true,验证失败返回false
*/
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, postcode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.vondear.rxtools.RxActivityUtils;

public class ActivityBase extends FragmentActivity {

public ActivityBase mContext;
Expand All @@ -12,6 +14,7 @@ public class ActivityBase extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
RxActivityUtils.addActivity(this);
}

@Override
Expand Down

0 comments on commit bbc741b

Please sign in to comment.