每 5 秒运行一次警报,并使用 AsyncTask 和服务检查阈值
我正在编写一个应用程序,需要每 5 秒进行一些工作,如果这些计算高于阈值,则应该启动一个 Intent,它将加载一个新的 Activity(或者如果可能的话,该 Activity 将由进程本身启动) 。
到目前为止,我正在采用 Wrox 的 Professional Android 2 Application Development 书中的方法,其中规定使用 AlarmManager 和 AsyncTask 来刷新数据。 http://www .wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html ->示例项目 ->第 9 章 Earthquake 4
到目前为止,这就是我所掌握的:
Activity.onCreate 启动服务
onCreate 方法从已启动的服务设置带有 PendingIntent 的警报参数(不启动 已启动的服务中的 onCreate 方法使用 PendingIntent 设置 Alarm 参数(尚未
启动服务的onStartCommand创建警报并创建AsyncTask(带执行)
AsynchTask.doInBackground进行一些计算,如果阈值已超过,它会广播一个新的 Intent 并尝试停止 Alarm,如果未超过阈值则不执行任何操作。
服务从 AsyncTask 的 onPostExecute 停止。
BroadcastReceiver 的 onReceive 方法处理 2) 中的 PendingIntent 并启动服务,如下所示:
Intent startIntent = new Intent(context, MyService.class);
context.startService(startIntent);
现在警报还没有停止,我不太确定这是处理此类需求的最佳架构,您有什么经验可以分享吗? 我知道其他选项,如 IntentService、处理程序的使用、计时器,但这也是一种干净的方法,但是......它的性能高吗? 此致。吉列尔莫.
I'm writing an application which needs to make some work every 5 seconds, if those calculations are above a threshold an Intent should be launch which will load a new Activity (or maybe if it's possible the activity will be launched by the process itself).
So far, I'm taking the approach in Professional Android 2 Application Development book from Wrox, which states for refreshing data use an AlarmManager and AsyncTask.
http://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html -> Example Projects -> Chapter 9 Earthquake 4
This is what I have so far:
Activity.onCreate launches a service
onCreate method from the launched service sets Alarm parameters with PendingIntent (doesn't launch it yet).
onStartCommand of the launched service creates the alarm and creates the AsyncTask (with execute)
AsynchTask.doInBackground does some calculations and if the threshold has been passed, it broadcasts an new Intent and tries to stop the Alarm, if the threshold is not passed then nothing is done.
The service is stopped from onPostExecute of the AsyncTask.
onReceive method of a BroadcastReceiver handles the PendingIntent from 2) and launches the service like this:
Intent startIntent = new Intent(context, MyService.class);
context.startService(startIntent);
Right now the Alarm is not stopping, and I'm not quite sure this is the best architecture to handle this kind of needs, any experiences you can share?
I know about other options like IntentService, Usage of Handlers, Timers, but this is a clean way as well, but...Is its performance high?
Best Regards. Guillermo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在java中查看这个类
http://download .oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html
我认为你可以使用!
look at this class in java
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html
I think you can use!!
使用允许您启动 Alarm 的 PendingIntent,您可以在 AlarmManager 中调用 cancel(),它将停止。
Using the PendingIntent which let you start your Alarm, you can call cancel() in AlarmManager and it will stop.