繁体   English   中英

AlarmManager正在阻止主线程

[英]AlarmManager is blocking main thread

我已经实现了一个调用服务的AlarmManager。 问题是,尽管我在AsyncTask中启动它,但它阻塞了主线程。 这是我的AsyncTask的来源:

private class NotificationsServiceTask extends AsyncTask<Void, Void, Void> {
    private AlarmManager alarmMgr;
    private PendingIntent pi;

    @Override
    protected Void doInBackground(Void... params) {
        alarmMgr = (AlarmManager) LoginActivity.this.getSystemService(Context.ALARM_SERVICE);
        Intent serviceIntent = new Intent(LoginActivity.this, Service.class);
        pi = PendingIntent.getService(LoginActivity.this, 0, serviceIntent, 0);
        alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 120000, pi);
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
} 

我需要异步执行此操作,因为它阻塞了我的主线程。

您可以在AsyncTask中设置警报没关系。 警报管理器将始终在主线程上启动您的服务,因为这就是服务的工作方式。

要解决您的问题,您将需要修改警报开始的服务以创建要运行的AsyncTask

我知道不是链接服务器场,但Commonsguy编写了WakeFullIntentService

他明确地使用警报接收器进行了覆盖。 很棒,值得签出。 这将使来自警报接收器的请求排队,在后台运行并唤醒设备,同时在单独的线程上运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM