一、网络请求工具类: 最重要的是对http协议的理解,HttpUrlConnection 

package com.example.tuhuadmin.fanxinglei.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created on 2016/8/15.
 * Author:crs
 * Description:网络请求工具类
 */
public class HttpUtils {

    //设置网络连接超时时间
    public static final int TIMEOUT_IN_MILLIONS = 5000;


    //设置请求结果回调
    public interface CallBack {
        void onRequestComplete(String result);
    }

    /**
     * 异步get请求
     *
     * @param urlStr
     * @param callBack 以接口对象为参数,特别好用
     */
    public static void doGetAsy(final String urlStr, final CallBack callBack) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = doGet(urlStr);
                if (callBack != null) {
                    callBack.onRequestComplete(result);
                }
            }
        }).start();
    }

    /**
     * 异步post请求
     *
     * @param urlStr
     * @param params
     * @param callBack
     */
    public static void doPostAsy(final String urlStr, final String params, final CallBack callBack) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = doPost(urlStr, params);
                if (callBack != null) {
                    callBack.onRequestComplete(result);
                }
            }
        }).start();
    }

    /**
     * 向指定url发送post方式的请求
     *
     * @param urlStr
     * @param params
     * @return
     */
    private static String doPost(String urlStr, String params) {
        URL url;
        String result = "";
        BufferedReader bufferedReader = null;
        PrintWriter printWriter = null;
        try {
            url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
            conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
            //设置一系列的请求属性,让服务器端知道(我发送了什么类型的数据)
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "keep-Alive");
            conn.setRequestProperty("charset", "utf-8");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //发送POST请求必须设置如下两行
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //对请求参数的处理
            if (params != null && !params.trim().equals("")) {
                //获取URLConnection对象对应的输出流
                OutputStream os = conn.getOutputStream();
                printWriter = new PrintWriter(os);
                // 发送请求参数
                printWriter.print(params);
                printWriter.flush();
            }
            //定义BufferedReader输入流来读取URL的响应
            InputStream is = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);
            //主要是为了每次读取一行
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
            //使用finally块来关闭输出流、输入流
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    /**
     * get请求
     *
     * @param urlStr
     * @return
     */
    private static String doGet(String urlStr) {
        URL url = null;
        HttpURLConnection conn = null;
        InputStream is = null;//输入流
        ByteArrayOutputStream baos = null; //输出流
        try {
            url = new URL(urlStr);
            //获取一个连接对象
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "keep-Alive");
            //获取响应码 说明请求成功
            if (conn.getResponseCode() == 200) {
                //获取一个输入流
                is = conn.getInputStream();
                //创建一个字节数组输出流
                baos = new ByteArrayOutputStream();
                int len = -1;
                //获取一个字节数组
                byte[] bytes = new byte[1024];
                while ((len = is.read(bytes)) != -1) {
                    baos.write(bytes, 0, len);
                }
                baos.flush();
                //返回字符串
                return baos.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //处理管理流的逻辑
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }
        return null;
    }
}

二、网络工具类: 是否有网络连接,是否是wifi连接,打开网络设置界面 ConnectivityManager

package com.example.tuhuadmin.fanxinglei.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 on 2016/8/15.
 * Author:crs
 * Description:网络工具类
 */
public class NetUtils {

    /**
     * 判断网络是否连接
     *
     * @param context
     * @return
     */
    public static boolean isConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                if (activeNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是不是WiFi连接
     *
     * @param context
     * @return
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
        }
        return false;
    }

    /**
     * 打开网络设置界面
     *
     * @param activity
     */
    public static void openSetting(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);
    }

}



三、软键盘工具类,打开或者关闭软键盘 InputMethodManager

package com.example.tuhuadmin.fanxinglei.utils;

import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created on 2016/8/15.
 * Author:crs
 * Description:软键盘工具类,需要事件触发
 */
public class KeyBoardUtils {

    /**
     * 打开软键盘
     *
     * @param editText
     * @param context
     */
    public static void openKeybord(EditText editText, Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     * @param editText
     * @param context
     */
    public static void closeKeybord(EditText editText, Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
}

四、app相关工具类 PackageManager  获取应用名称,获取应用的版本名称

package com.example.tuhuadmin.fanxinglei.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created on 2016/8/15.
 * Author:crs
 * Description:app工具类
 */
public class AppUtils {

    //获取应用程序名称
    public static String getAppName(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            //这种方式是可以的
            //String name = applicationInfo.loadLabel(pm).toString();
            int labelRes = applicationInfo.labelRes;
            String name = context.getResources().getString(labelRes);
            return name;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    //获取应用程序版本名称
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String versionName = packageInfo.versionName;
            return versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }
}



五、屏幕工具类  WindowManager、Display

package com.example.tuhuadmin.fanxinglei.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created on 2016/8/1.
 * Author:crs
 * Description:屏幕工具类
 */
public class ScreenUtils {
    //获取屏幕的宽度
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wm.getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        return width;
    }

    //获取屏幕的高度
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display defaultDisplay = wm.getDefaultDisplay();
        int height = defaultDisplay.getHeight();
        return height;
    }

    //获取状态栏的高度
    public static int getStatusHeight(Context context) {
        int statusHeight = -1;
        //使用反射,可能会出现类找不到的异常ClassNotFoundException
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            String status_bar_height = clazz.getField("status_bar_height").get(object).toString();
            int height = Integer.parseInt(status_bar_height);
            //转化成px返回
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    //获取当前屏幕截图,包括状态栏
    public static Bitmap getSnapshot(Activity activity) {
        Window window = activity.getWindow();
        View view = window.getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        int screenWidth = getScreenWidth(activity);
        int screenHeight = getScreenHeight(activity);
        Bitmap bp;
        bp = Bitmap.createBitmap(bitmap, 0, 0, screenWidth, screenHeight);
        view.destroyDrawingCache();
        return bp;
    }
}




六、SD卡工具类, Environment类

package com.example.tuhuadmin.fanxinglei.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.File;

/**
 * Created on 2016/8/15.
 * Author:crs
 * Description:sd卡工具类
 */
public class SDCardUtils {

    //判断sd卡是否可用
    public static boolean isSDCardEnable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    //获取sd卡路径
    public static String getSDCardPath() {
        File file = Environment.getExternalStorageDirectory();
        String path = file.getAbsolutePath() + File.separator;
        return path;
    }

    //获取系统存储路径
    public static String getRootDirectoryPath() {
        File rootDirectory = Environment.getRootDirectory();
        String path = rootDirectory.getAbsolutePath();
        return path;
    }

    //获取sd卡的剩余存储空间  单位byte
    public static long getSDCardAllSize() {
        StatFs statFs = new StatFs(getSDCardPath());
        //获取空闲的数据块的数量
        long availableBlocksLong = statFs.getAvailableBlocks();
        //获取单个数据块的大小
        long blockSize = statFs.getBlockSize();
        return availableBlocksLong * blockSize;
    }
}

七、单位转化工具类DisplayMetrics

package com.example.tuhuadmin.fanxinglei.utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;


/**
 * Created on 2016/8/14.
 * Author:crs
 * Description:XXX
 */
public class DensityUtils {
    public static int sp2px(Context context, int i) {
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        float fontScale = displayMetrics.scaledDensity;
        return (int) (i * fontScale + 0.5f);
    }


    //dp转化成px   其他的模仿就行了  因为1dp=1sp的
    public static int dp2px(Context context, float dpValue) {
        Resources resources = context.getResources();
        //用于存储显示的通用信息,如显示大小,分辨率和字体。
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        //获取转化新系数(这里不太懂 要好好搞一下)
        float scale = displayMetrics.density;
        //在160dpi里面
        return (int) (dpValue * scale + 0.5f);
    }

    public static int px2dp(Context context, float pxValue) {
        Resources resources = context.getResources();
        //用于存储显示的通用信息,如显示大小,分辨率和字体。
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        //获取转化新系数(这里不太懂 要好好搞一下)
        float scale = displayMetrics.density;
        //在160dpi里面
        return (int) (pxValue / scale + 0.5f);
    }
}

八,首选项工具类

package com.example.tuhuadmin.fanxinglei.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created on 2016/8/2.
 * Author:crs
 * Description:首选项工具类
 */
public class SPUtils {
    //存储数据需要editor对象,取数据直接首选项对象就好了。
    private static final String SHARE_PREFER_NAME = "config";
    private static SharedPreferences mSharedPreferences;

    public static void putBoolean(Context context, String key, boolean value) {
        if (mSharedPreferences == null) {
            //私有模式
            mSharedPreferences = context.getSharedPreferences(SHARE_PREFER_NAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(SHARE_PREFER_NAME, Context.MODE_PRIVATE);
        }
        //如果获取不到就显示默认值
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public static void putString(Context context, String key, String value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(SHARE_PREFER_NAME, Context.MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = mSharedPreferences.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public static String getString(Context context, String key, String defaultValue) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(SHARE_PREFER_NAME, Context.MODE_PRIVATE);
        }
        //如果获取不到就显示默认值
        return mSharedPreferences.getString(key, defaultValue);
    }
}

九、log工具类

package com.example.tuhuadmin.fanxinglei.utils;

import android.util.Log;

/**
 * Created on 2016/8/1.
 * Author:crs
 * Description:XXX
 */
public class LogUtils {
    public static boolean isDebug = true;

    public static void e(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }

    }

    public static void w(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }

    public static void v(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }


    public static void d(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (isDebug) {
            Log.i(tag, msg);
        }
    }


}



十、Toast工具类

package com.example.tuhuadmin.fanxinglei.utils;

import android.content.Context;
import android.widget.Toast;

/**
 * Created on 2016/8/1.
 * Author:crs
 * Description:XXX
 */
public class ToastUtils {
    public static void showLong(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public static void showShort(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT);
    }
}