• 工具类总结---(七)---之通用工具


           这里所谓的通用工具,就是表示经常会用到的方法,比如说判断网络的状态,判断sd是否存在,弹出toast等等。

        

    package cgjr.com.cgjr.utils;
    
    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.os.Environment;
    import android.text.TextUtils;
    import android.util.Log;
    import android.widget.Toast;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.io.Writer;
    
    import cgjr.com.cgjr.CGApplication;
    import cgjr.com.cgjr.data.ErrorMsg;
    
    /**
     * Created by Administrator on 2016/4/13.
     */
    public class UtilityUtils {
    
        /**
         * 判断是否有网络连接
         *
         * @param context
         * @return
         */
        public static boolean isNewWorkConnected(Context context) {
            if (context != null) {
                ConnectivityManager mConnectivityManager =
                        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (mConnectivityManager != null) {
                    NetworkInfo mobileInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    NetworkInfo wifiInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    if ((mobileInfo == null || !mobileInfo.isAvailable()) && (wifiInfo == null || !wifiInfo.isAvailable())) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            return true;
        }
    
        public static void makeText_short(Context context, String msg) {
            if (!TextUtils.isEmpty(msg) && context != null) {
                Toast.makeText(context, TextUtils.isEmpty(msg) ? "" : msg, Toast.LENGTH_LONG).show();
            }
        }
    
        /**
         * @param activity 支持页面
         * @param obj      解析数据图像,解析为Json对象,以便进行解析操作
         * @return
         * @throws JSONException
         */
        public static JSONObject parseObj(Context activity, Object obj) throws JSONException {
            return new JSONObject(obj.toString());
        }
    
        /**
         * 判断SD卡是否存在
         *
         * @return
         */
        public static boolean isHaveSD() {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                return true;
            } else {
                return false;
            }
        }
    
        /**
         * 保存错误信息到本地数据库中
         *
         * @param errorMsg 错误信息
         */
        public static void saveExceptionInfo(Throwable errorMsg) {
            LogUtils.i("saveExceptionInfo", "保存错误日志:" + getErrorInfo(errorMsg));
            ErrorMsg error = new ErrorMsg(CGApplication.getApp(), getErrorInfo(errorMsg), Log.ERROR);
            error.save();
        }
    
        /**
         * 获取错误信息
         *
         * @param arg1
         * @return
         */
        public static String getErrorInfo(Throwable arg1) {
            Writer writer = new StringWriter();
            PrintWriter pw = new PrintWriter(writer);
            arg1.printStackTrace(pw);
            pw.close();
            String error = writer.toString();
            return error;
        }
    
    
    
    }

         上面的保存错误的日志,到本地数据库,则涉及到操作本地数据库的问题。

     

        

    package cgjr.com.cgjr.data;
    
    import android.os.Build;
    import android.util.Log;
    
    import org.litepal.crud.DataSupport;
    
    import java.util.Date;
    
    import cgjr.com.cgjr.CGApplication;
    import cgjr.com.cgjr.constants.Constants;
    import cgjr.com.cgjr.utils.DateUtil;
    
    /**
     * 程序错误类
     * Created by Administrator on 2016/4/13.
     */
    public class ErrorMsg extends DataSupport {
        public ErrorMsg() {
        }
    
        public ErrorMsg(CGApplication cgApp) {
            setNet(cgApp.getNetWorkStatus());
            setuId(cgApp.getUserId());
        }
    
    
        public ErrorMsg(CGApplication cgApp, String msg, int log) {
            setNet(cgApp.getNetWorkStatus());
            setuId(cgApp.getUserId());
            setMsg(msg);
            setLog(log);
        }
    
        /**
         * Priority 日志级别
         */
        private int log = Log.INFO;
    
        /**
         * 用户ID
         */
        private String uId;
    
    
        /**
         * netstatus; //网络可用状态,0 表示无网络可用,1 表示 wifi网络可用  2 表示 手机网络可用
         */
        private int net = 0;
    
        /**
         * 手机型号
         */
        private String mt = Build.MODEL;
    
        /**
         * 系统 版本号
         */
        private String sv = Build.VERSION.RELEASE;
    
        /**
         * os
         */
        private String os = "android";
    
        /**
         * app version
         */
        private String av = Constants.version_;
    
        /**
         * 时间
         */
        private String time = DateUtil.formatDatetoString(new Date(), DateUtil.ISO8601_DATETIME_PATTERN);
    
        /**
         * 错误内容
         */
        private String msg;
    
    
        public int getLog() {
            return log;
        }
    
        public void setLog(int log) {
            this.log = log;
        }
    
        public String getuId() {
            return uId;
        }
    
        public void setuId(String uId) {
            this.uId = uId;
        }
    
        public int getNet() {
            return net;
        }
    
        public void setNet(int net) {
            this.net = net;
        }
    
        public String getMt() {
            return mt;
        }
    
        public void setMt(String mt) {
            this.mt = mt;
        }
    
        public String getSv() {
            return sv;
        }
    
        public void setSv(String sv) {
            this.sv = sv;
        }
    
        public String getOs() {
            return os;
        }
    
        public void setOs(String os) {
            this.os = os;
        }
    
        public String getAv() {
            return av;
        }
    
        public void setAv(String av) {
            this.av = av;
        }
    
        public String getTime() {
            return time;
        }
    
        public void setTime(String time) {
            this.time = time;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
    }

      

      

  • 相关阅读:
    自适应Simpson积分
    斜率优化
    ORM的单表增删改查
    MTV模型—urls和view
    迭代器与生成器
    s7day2学习记录
    s7day1学习记录
    AI车牌识别涉及哪些技术?它是如何改变行业的?
    技术分享:人脸识别究竟是如何完成的?
    TSINGEE青犀视频行人检测景区测试时视频流切换本地背景音乐无法播放问题优化
  • 原文地址:https://www.cnblogs.com/beigongfengchen/p/5454559.html
Copyright © 2020-2023  润新知