Android 最简单的测试UI卡顿

Posted 水柠檬QAQ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 最简单的测试UI卡顿相关的知识,希望对你有一定的参考价值。

就两个类:

public class BlockDetectByPrinter {
private static final String START = ">>>>> Dispatching";
private static final String END = "<<<<< Finished";
public static void start(){
Looper.getMainLooper().setMessageLogging(new Printer() {
@Override
public void println(String x) {
if(x.startsWith(START)){
LogMonitor.getsInstance().startMonitor();
}
if(x.startsWith(END)){
LogMonitor.getsInstance().startMonitor();
}
}
});
}
}


public class LogMonitor {
private static LogMonitor sInstance = new LogMonitor();
private HandlerThread mLogThread = new HandlerThread("log");
private Handler mIoHandler;
private static final long TIME_BLOCK = 2000L;

private LogMonitor(){
mLogThread.start();
mIoHandler = new Handler(mLogThread.getLooper());
}
private static Runnable mLogRunnable = new Runnable() {
@Override
public void run() {
StringBuilder sb = new StringBuilder();
StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace();
for(StackTraceElement s:stackTrace){
sb.append(s.toString()+"\n");
}
LogUtil.e("LogMonitor",sb.toString());
}
};
public static LogMonitor getsInstance(){
return sInstance;
}
// public boolean isMonitor(){
// return mIoHandler.hasCallbacks(mLogRunnable);
// }
public void startMonitor(){
mIoHandler.postDelayed(mLogRunnable,TIME_BLOCK);
}
public void removeMonitor(){
mIoHandler.removeCallbacks(mLogRunnable);
}

}
然后在application的onCreate方法使用
BlockDetectByPrinter.start();





























































以上是关于Android 最简单的测试UI卡顿的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 中的 Fragment 访问 UI 元素?

Android的UI卡顿

Android的UI卡顿

Android开发之——怎么检测UI卡顿?(线上及线下)

android UI卡顿问题学习

最全最系统的 Android 界面性能调优资料