打印方法路徑方法一
/**
* 打印方法路徑
*/
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);
}
}