打印日志

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 打印主要信息

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值