【发布时间】:2021-11-02 09:06:15
【问题描述】:
获取 ANR,执行 Intent.ACTION_SCREEN_ON 的阻塞/重度调用。下面是代码
private static BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_ON)) {
// Blocking operation
}
}
}
为了避免 ANR,我打算将阻塞操作移到工作线程中。下面的代码是否有助于避免 ANR?
private static BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_ON)) {
new Handler().post(new Runnable() {
@Override
public void run() {
//Blocking operation
}
});
}
}
}
【问题讨论】:
标签: android android-intent broadcastreceiver android-handler android-anr-dialog