1、日志常用工具类,LogUtils.java
package cn.jon.utils;
import android.util.Log;
/**
* Created by Administrator on 2017/3/24.
*/
public class LogUtils {//日志显示类
public static boolean isDebug=true;
private static final String TAG="jon";
public static void i(String msg)
{
if (isDebug)
{
Log.i(TAG,msg);
}
}
public static void d(String msg)
{
if (isDebug)
{
Log.d(TAG, msg);
}
}
public static void e(String msg)
{
if (isDebug)
{
Log.e(TAG, msg);
}
}
public static void v(String msg)
{
if (isDebug)
{
Log.v(TAG, msg);
}
}
public static void w(String msg)
{
if (isDebug)
{
Log.w(TAG, msg);
}
}
}
2、toast的常用工具类,ToastUtils.java
package cn.jon.utils;
import android.content.Context;
import android.widget.Toast;
/**
* Created by Administrator on 2017/3/24.
*/
public class ToastUtils {
private static Toast toast=null;//全局Toast
private static final String SHOW_FAILURE="failure";
private static final String SHOW_SUCCESS="success";
/**
* 显示消息
* @param context
* @param msg
*/
public static void show(Context context,String msg)
{
if (null==toast)
{
toast=Toast.makeText(context,msg,Toast.LENGTH_SHORT);
}else
{
toast.setText(msg);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
}
/**
* 提示消息失败
* @param context
*/
public static void showFailure(Context context)
{
if (null==toast)
{
toast=Toast.makeText(context,SHOW_FAILURE,Toast.LENGTH_SHORT);
}else {
toast.setText(SHOW_FAILURE);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
}
/**
* 提示消息成功
* @param context
*/
public static void showSuccess(Context context)
{
if (null==toast)
{
toast=Toast.makeText(context,SHOW_SUCCESS,Toast.LENGTH_SHORT);
}else {
toast.setText(SHOW_SUCCESS);
toast.setDuration(Toast.LENGTH_SHORT);
}
toast.show();
}
}
3、获取屏幕的宽高,状态栏的高及取消全屏设置,ScreenDisplayUtils.java
package cn.jon.utils;
import android.app.Activity;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
/**
* Created by Administrator on 2017/3/24.
*/
public class ScreenDisplayUtils {
/**
* 获取屏幕宽度
* @param activity
* @return
*/
public static int getWidth(Activity activity)
{
DisplayMetrics displayMetrics=new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.widthPixels;
}
/**
* 获取屏幕高度
* @param activity
* @return
*/
public static int getHeight(Activity activity)
{
DisplayMetrics displayMetrics=new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
/**
* 设置全屏
* @param activity
*/
public static void setFullScreen(Activity activity)
{
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/**
* 取消全屏设置
* @param activity
*/
public static void cancelFullScreen(Activity activity)
{
WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().setAttributes(attrs);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
/**
* 获取状态栏的高度
* @param activity
* @return
*/
public static int getStatusBarHeight(Activity activity) {
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
return statusBarHeight;
}
/**
* 获取标题栏的高度
* @param activity
* @return
*/
public static int getTitleBarHeight(Activity activity)
{
int contentTop=activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
return contentTop-getStatusBarHeight(activity);
}
}
4、时间格式工具类,TimeUtils.java
package cn.jon.utils;
import android.text.format.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Administrator on 2017/4/1.
*/
public class TimeUtil {
/**
* 获取系统当前时间 格式 2017-04-01 17:30:30
* @return
*/
public static String getAll() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%Y-%m-%d %H:%M:%S");
}
/**
* 获取系统当前日期 格式 2017-04-01
* @return
*/
public static String getDate() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%Y-%m-%d");
}
/**
* 获取系统当前时间 格式 17:30:30
* @return
*/
public static String getTime() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%H:%M:%S");
}
/**
* 获取系统当前年份 格式 2017
* @return
*/
public static String getYear() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%Y");
}
/**
* 获取系统当前月份 格式 03
* @return
*/
public static String getMouth() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%m");
}
/**
* 获取今天是什么日子 格式 02
* @return
*/
public static String getDay() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%d");
}
/**
* 获取系统当前小时 格式 17
* @return
*/
public static String getHour() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%H");
}
/**
* 获取系统当前分钟 格式 59
* @return
*/
public static String getMinute() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%M");
}
/**
* 获取系统当前秒数 30
* @return
*/
public static String getSeconds() {
Time localTime = new Time("Asia/Hong_Kong");
localTime.setToNow();
return localTime.format("%S");
}
/**
* 时间戳转时间
* @param s
* @return
*/
public static String longToTime(String s) {
long time = Long.parseLong(s);
Date date = new Date(time * 1000);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(date);
}
/**
* 对时间进行编码 传入的时间格式 2017-04-01 17:05:59
* @param time
* @return
*/
public static String decode(String time) {
try {
int iDayNow = Integer.parseInt(getDay());
int iDayRec = Integer.parseInt(time.substring(time.lastIndexOf("-") + 1, time.lastIndexOf("-") + 3));
int iMouthNow = Integer.parseInt(getMouth());
int iMouthRec = Integer.parseInt(time.substring(time.indexOf("-") + 1, time.indexOf("-") + 3));
int iYearNow = Integer.parseInt(getYear());
int iYearRec = Integer.parseInt(time.substring(0, time.indexOf("-")));
if (iYearNow == iYearRec) {
if (iMouthNow == iMouthRec) {
if (iDayNow == iDayRec) {
return time.substring(time.lastIndexOf("-") + 3, time.length() - 3);
} else if (iDayNow - iDayRec == 1) {
return "昨天" + time.substring(time.lastIndexOf("-") + 3, time.length() - 3);
} else if (iDayNow - iDayRec == 2) {
return "前天" + time.substring(time.lastIndexOf("-") + 3, time.length() - 3);
} else {
return iDayNow - iDayRec + " 天前" + time.substring(time.lastIndexOf("-") + 3, time.length() - 3);
}
} else {
return (iMouthNow - iMouthRec) + " 个月前";
}
} else {
return (iYearNow - iYearRec) + " 年前";
}
} catch (Exception e) {
return time;
}
}
}
5、对话框常用类,DialogUtils.java
package cn.jon.utils;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
/**
* Created by Administrator on 2017/4/1.
*/
public class DialogUtil {
public static Dialog dialog;
public static ProgressDialog progressDialog;
/**
* 显示进度对话框
* @param activity
*/
public static void progress(Activity activity) {
progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage("处理中...");
progressDialog.show();
}
/**
* 显示对话框,自定义内容
* @param activity
* @param title
*/
public static void progress(Activity activity, String title) {
progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage(title);
progressDialog.show();
}
/**
* 关闭对话框
*/
public static void cancel() {
try {
if (dialog != null) {
dialog.cancel();
}
if (progressDialog != null) {
progressDialog.cancel();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
6、文件常用工具类,FileUtils.java
package cn.jon.utils;
import android.graphics.Bitmap;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2017/4/1.
*/
public class FileUtils {
public static String downPath = "cn/jon/Down/";
public static String cachePath = "cn/jon/Cache/";
public static String imagePath = "cn/jon/Image/";
/**
* 判断是否有sd卡
* @return
*/
public static boolean isSDExist() {
String str = Environment.getExternalStorageState();
return str.equals(Environment.MEDIA_MOUNTED);
}
/**
* 获取根目录路径
* @return
*/
public static String getRootPath() {
if (isSDExist()) {
return Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
} else {
return Environment.getDataDirectory().getAbsolutePath() + "/data/";
}
}
/**
* 获取缓存路径
* @return
*/
public static String getCachePath() {
return getRootPath() + cachePath;
}
/**
* 获取图片存放路径
* @return
*/
public static String getImagePath() {
return getRootPath() + imagePath;
}
/**
* 获取下载路径
* @return
*/
public static String getDownPath() {
return getRootPath() + downPath;
}
/**
* 创建图片文件夹
*/
public static void createImagePath() {
File file = new File(getImagePath());
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 创建缓存文件夹
*/
public static void createCachePath() {
File file = new File(getCachePath());
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 创建下载文件夹
*/
public static void createDownPath() {
File file = new File(getDownPath());
if (!file.exists()) {
file.mkdirs();
}
}
/**
* 创建一张jpg图片
* @param name
* @param bitmap
* @return
*/
public static String createJpgByBitmap(String name, Bitmap bitmap) {
String path = FileUtils.getImagePath() + name + ".jpg";
try {
File file = new File(path);
if (file.exists()) {
file.delete();
}
FileOutputStream fileOutputStream;
file.createNewFile();
fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
}
7、网络设置常用的工具类,NetUtils.java
package cn.jon.utils;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by Administrator on 2017/4/1.
*/
public class NetUtils {
/**
* 检查是否存在网络连接
* @param context
* @return
*/
public static boolean isConnected(Context context)
{
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (null != connectivity)
{
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected())
{
if (info.getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
return false;
}
/**
* 检查是否是通过wifi连接网络
* @param context
* @return
*/
public static boolean isWifiConnected(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
}
/**
* 打开设置网络界面,调用settings相关的类
* @param activity
*/
public static void openWirelessSettings(Activity activity)
{
Intent intent = new Intent("/");
ComponentName cm = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(cm);
intent.setAction("android.intent.action.VIEW");
activity.startActivityForResult(intent, 0);
}
}