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