【问题标题】:Avoid ANR on SCREEN_ON using Handler Thread使用处理程序线程避免 SCREEN_ON 上的 ANR
【发布时间】: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


    【解决方案1】:

    没有。 BroadcastReceivers 是短暂的。无法保证托管您的BroadcastReceiver 的操作系统进程的寿命足以执行此代码。不仅如此,您还将在主 (UI) 线程上运行此代码,这是您无法阻止的(您的 Runnable 不会在“工作线程”中运行,它将在主 (UI) 线程中运行)。

    您的BroadcastReceiver 应该启动一个Service,它可以运行一个后台(工作)线程来执行您的阻塞操作。或者您可以使用JobScheduler 来安排此操作(如果它适合您的目的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      • 2016-05-27
      相关资源
      最近更新 更多