ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.startActivity:1005 android.app.ContextImpl.startActivity:994 android.content.ContextWrapper.startActivity:403 应用的包名和类名:116
Android中的ContextImpl竟然打印了Service和BroadcastReceiver的调用栈
看源码:
根据“Calling a method in the system process without”查找代码。
1.ContextImpl.java
frameworks/base/core/java/android/app/ContextImpl.java
在startActivity的时候调用了warnIfCallingFromSystemProcess方法,,warnIfCallingFromSystemProcess会通过Debug.getCallers(5)来打印当前应用的任务栈
991 @Override
992 public void startActivity(Intent intent) {
993 warnIfCallingFromSystemProcess();
994 startActivity(intent, null);
995 }
2239 private void warnIfCallingFromSystemProcess() {
2240 if (Process.myUid() == Process.SYSTEM_UID) {
2241 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
2242 + Debug.getCallers(5));
2243 }
2244 }
2.Deubug
frameworks/base/core/java/android/os/Debug.java
2470 /**
2471 * Return a String describing the calling method and location at a particular stack depth.
2472 * @param callStack the Thread stack
2473 * @param depth the depth of stack to return information for.
2474 * @return the String describing the caller at that depth.
2475 */
2476 private static String getCaller(StackTraceElement callStack[], int depth) {
2477 // callStack[4] is the caller of the method that called getCallers()
2478 if (4 + depth >= callStack.length) {
2479 return "<bottom of call stack>";
2480 }
2481 StackTraceElement caller = callStack[4 + depth];
2482 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2483 }
2484
2485 /**
2486 * Return a string consisting of methods and locations at multiple call stack levels.
2487 * @param depth the number of levels to return, starting with the immediate caller.
2488 * @return a string describing the call stack.
2489 * {@hide}
2490 */
2491 @UnsupportedAppUsage
2492 public static String getCallers(final int depth) {
2493 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2494 StringBuffer sb = new StringBuffer();
2495 for (int i = 0; i < depth; i++) {
2496 sb.append(getCaller(callStack, i)).append(" ");
2497 }
2498 return sb.toString();
2499 }
主要代码就是:Thread获取当前线程的堆栈元素
final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
StackTraceElement caller = callStack[4 + depth];
2482 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
StackTraceElement 打印主要信息