在API<上收到SMS时,请执行长时间运行的操作. 21岁 [英] Perform long running operations when receiving SMS on API < 21

查看:131
本文介绍了在API<上收到SMS时,请执行长时间运行的操作. 21岁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序将侦听传入的SMS消息,执行一些可能会长时间运行的操作,并在满足某些条件时发回答复.

I'm building an app that will listen for incoming SMS messages, perform some potentially long running operations and send back a reply if some conditions are met.

我已经使用BroadcastReceiver使侦听部分正常工作,但是我不确定如何/在何处执行可能长时间运行的操作. Android开发人员文档指出

I've got the listening part working using a BroadcastReceiver, but I'm not sure how/where to perform the potentially long running operations. The Android Developer Documentation states that

在onReceive()之后,系统可以随时终止进程以执行以下操作: 回收内存,并以此终止生成的线程 在运行过程中.为避免这种情况,您应该致电 goAsync()(如果您需要更多时间来处理广播 后台线程)或使用以下命令从接收方安排JobService JobScheduler,因此系统知道该过程将继续 进行积极的工作.

After onReceive(), the system can kill the process at any time to reclaim memory, and in doing so, it terminates the spawned thread running in the process. To avoid this, you should either call goAsync() (if you want a little more time to process the broadcast in a background thread) or schedule a JobService from the receiver using the JobScheduler, so the system knows that the process continues to perform active work.

还有:

在接收者的onReceive()方法中调用goAsync()并传递 将BroadcastReceiver.PendingResult传递到后台线程.这保持 从onReceive()返回后,广播处于活动状态.但是,即使 通过这种方法,系统希望您完成广播 非常快(不到10秒).它确实允许您将工作移至 另一个线程以避免主线程故障.

Calling goAsync() in your receiver's onReceive() method and passing the BroadcastReceiver.PendingResult to a background thread. This keeps the broadcast active after returning from onReceive(). However, even with this approach the system expects you to finish with the broadcast very quickly (under 10 seconds). It does allow you to move work to another thread to avoid glitching the main thread.

此外,JobScheduler在API<上不可用. 21.

Furthermore, JobScheduler is not available on API < 21.

那我如何在API 16-20上实现呢?

So how would I achieve this on API 16 - 20?

推荐答案

只需在您的BroadcastReceiver中启动Service即可执行长时间运行的操作并将结果发送回去.您可以使用IntentService或常规的Service.

Just start a Service in your BroadcastReceiver to do the long-running operation and send back the result. You could use an IntentService or a regular Service.

这篇关于在API&lt;上收到SMS时,请执行长时间运行的操作. 21岁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆