我在SDK
中集成了BlockCanary
,新增加了一个BlockObserver
用于后续上报问题堆栈到自己的服务器
public class BlockObserver implements BlockMonitor.BlockObserver{
@Override
public void onBlock(BlockInfo blockInfo) {
report(blockInfo);
}
private void report(BlockInfo blockInfo) {
Log.d("BlockReport", "getFrequentMethods: "
+ blockInfo.getFrequentMethods()
+ "getHeavyMethods: " + blockInfo.getHeavyMethods()
+ "getEnvInfo: " + blockInfo.getEnvInfo()
+ "getTopHeavyMethod: " + blockInfo.getTopHeavyMethod());
printThreadStackTrace();
}
private void printThreadStackTrace(){
final Thread mainThread = Looper.getMainLooper().getThread();
Log.d("BlockReport","\nmainThread: " + mainThread.getStackTrace().toString());
for (StackTraceElement stackTrace : mainThread.getStackTrace()){
Log.d("BlockReport","\nstackTraceElement: " + stackTrace);
}
// for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
// Thread thread = stackTrace.getKey();
// if (thread!=null) {
// StackTraceElement[] stack = stackTrace.getValue();
// for (StackTraceElement stackTraceElement : stack) {
// Log.d("BlockReport","\nstackTraceElement: " + stackTraceElement);
// }
//
// }
// }
}
}
然后我在SDK
初始化的时候注册自己的observer
public void start() {
...
BlockMonitor.registerBlockObserver(new BlockObserver());
BlockCanaryEx.install(new Config(context));
...
}
我在demoApp
中新增一个 2s 的sleep
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Button gcButton = (Button) findViewById(R.id.gc_button);
gcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// doGc();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("BlockReport", "onClick: Sleep->1000");
Toast.makeText(MainActivity.this,"GC Finished",Toast.LENGTH_SHORT).show();
}
});
}
但是为啥没有堆栈!
这个是我debug
时的blockInfo
对象
下面这个则是blockCanary
的UI
堆栈信息显示
发现两者都没有拿到堆栈信息!有谁知道这个是什么情况吗?