Android 工具类


打印方法路径方法一
    /**
     * 打印方法路径
     */
    public static void printMethodPath() {
        //new 一个异常类
        Exception exception = new Exception();
        //调用者上级类名
        Log.i(TAG, "Class0———>:" + exception.getStackTrace()[0].getClassName());
        //调用者上级的上级类名
        Log.i(TAG, "Class1———>:" + exception.getStackTrace()[1].getClassName());
        //调用者上级的方法名
        Log.i(TAG, "MethodName0———>:" + exception.getStackTrace()[0].getMethodName());
        //调用者上级的上级方法名
        Log.i(TAG, "MethodName1———>:" + exception.getStackTrace()[1].getMethodName());
        //当前方法行号
        Log.i(TAG, "line0———>:" + exception.getStackTrace()[0].getLineNumber());
        //调用方法行号
        Log.i(TAG, "line1———>:" + exception.getStackTrace()[1].getLineNumber());
    }
打印方法路径方法二
    /**
     * 打印方法路径
     * @param type 0:方法开始,1:方法结束,2:方法异常,3:方法
     */
    public static void printMethodPath(String type){
        Exception exception = new Exception();
        try {
            if ("0".equals(type)){
                type = "方法开始";
            }else if("1".equals(type)) {
                type = "方法结束";
            }else if("2".equals(type)) {
                type = "方法异常";
            } else {
                type = "方法";
            }
            //exception.getStackTrace()[0].getClassName():0为当前方法类名;1为调用者类名.
            Log.i(TAG,"Class———>:" + exception.getStackTrace()[1].getClassName() + exception.getStackTrace()[1].getMethodName() + type +  " line:" + exception.getStackTrace()[1].getLineNumber());
        } catch (Exception e) {
            e.printStackTrace();
            FileLog.e(TAG,e.getMessage(),e);
        }
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM