添加依赖:
implementation ‘com.github.markzhai:blockcanary-android:1.5.0‘
运行后会同时安装检测工具,主要检测UI线程运行卡顿现象
public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SystemClock.sleep(1000); } }
让主界面卡顿1秒
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); BlockCanary.install(this, new AppContext()).start(); } public class AppContext extends BlockCanaryContext { //默认卡顿阈值为1000ms public int provideBlockThreshold() { return 1000; } //输出的log public String providePath() { return "/blockcanary/"; } //支持文件上传 public void upload(File zippedFile) { throw new UnsupportedOperationException(); } //可以在卡顿提供自定义操作 @Override public void onBlock(Context context, BlockInfo blockInfo) { System.out.println("阻塞操作"); } } }
检测到卡顿超过1秒输出日志
<application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher"
声明name属性
原文地址:https://www.cnblogs.com/Ocean123123/p/11025102.html
时间: 2024-11-07 15:58:07