线程和设备方向 [英] Threads and device orientation

查看:129
本文介绍了线程和设备方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我主活动上按钮preSS启动名为吸气其他类和消气剂类创建1-60 000线(其中做网络的东西),所以它需要一定的时间,直到它们都被完成。我的问题是,当我转动我的设备,而那些线程正在运行,我的程序停止正常工作。在我的主要活动我有处理程序,每2秒会从消气类的数组,然后把这些值转换成当地的数组列表。

I got main Activity that on button press starts other class named Getter and that Getter class creates 1-60 000 threads (which do network stuff), so it takes some time until they all are completed. My problem is that when I rotate my device while those threads are running, my program stops working correctly. In my main Activity I have handler that every 2 seconds gets an array from Getter class and then puts those values into local array list.

我试过这个解决方案:

<一个href=\"http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998\">Background任务进度对话框,方向的改变 - 有没有100%的工作液

但发现它很复杂。所以现在我想知道是否有任何其他的解决我的问题。比如我可以继续线程在后台运行,即使我的方向变化?如果这是不可能的我可​​以编程锁定取向之一,这只是之前按钮是pressed,然后等待,直到线程完成,然后再作方向去与传感器?

but found it very complicated. So now I am wondering if there is any other solution to my problem. For instance can my threads continue running in background even if my orientation changes? If that is not possible can I programatically lock orientation to the one it was just before button was pressed, then wait until threads have completed then again make orientation to go with sensor?

推荐答案

我们的处理这种方式是从一开始粘异步任务 app.Service ,并允许异步任务与这又与任何人收听它广播经由广播接收器的框架的事件进行通信的父服务通信。我已经好歹来形容这一机制<一个href=\"http://stackoverflow.com/questions/11793166/show-progress-dialog-within-a-cursorloader-in-a-fragment-android/11794421#11794421\">here.

Our way of handling this is to start the async task from a sticky app.Service and allow the async task to communicate with the parent Service which in turn communicates with anyone listening to it's broadcast events via the BroadcastReceiver framework. I have gone someway to describe this mechanism here.

在改变方向时,你的 app.Activity 将被毁灭,但后台服务不会(也可以是 onLowMemory &安培;其他情况下无数,但它可能不会)。在重新创建活动,您可以检查,看看您的服务仍然通过我张贴的解决方案<运行href=\"http://stackoverflow.com/questions/8417885/android-fragments-retaining-an-asynctask-during-screen-rotation-or-configuratio/11628709#11628709\">here.根据该结果,你可以决定如何处理用户界面,重新显示/重新添加进度对话做或什么的,并重新注册接收器监听走出服务的事件。

On orientation change, your app.Activity will be destroyed but the background Service won't be (it can be onLowMemory & a myriad of other circumstances but it probably won't). On recreating the activity you can check to see if your service is still running via the solution I posted here. Based on that result you can decide what to do with the UI, redisplay/re-add the progress dialogue or whatever and re-register your receiver to listen for events coming out of the Service.

这些层之间的职责是这样的;

The responsibilities between these layers works like this;


  1. 的AsyncTask

    • 做的工作。

    • 报告后面有什么就看,当它是由触发事件进行(意图

    • 就是这样


  • 主机为AsyncTask的
  • 的方向不知情容器
  • 注册听众为#onCreate从AsyncTask的事件发出

  • 执行的AsyncTask的在#onStartCommand

  • 停止本身(见 stopSelf()),当它从AsyncTask的
  • 收到我已经完成事件
  • 火灾介绍给任何人听进度事件

  • Hosts an orientation unaware container for the AsyncTask
  • Registers listeners for events emanating from the AsyncTask in #onCreate
  • Executes the AsyncTask in #onStartCommand
  • Stops itself (see stopSelf()) when it receives the "I have finished" event from the AsyncTask
  • Fires events describing progress to anyone listening

  • 开始反过来启动AsyncTask的服务。

  • 注册听众为#onCreate从服务所产生的事件

  • 检查是否在服务过程中的onCreate已运行做出决定以工作是否正在进行或需求出发。

3元素与分立unconfused角色。这就是我们喜欢的方式。用这种方法唯一的细微差别是使用了 BroadcastReceivers的。如果你习惯使用这些,那么你是金色的。

3 elements with discrete unconfused roles. That's the way we like it. The only nuance with this approach is the use of the BroadcastReceivers. If you are comfortable with those then you are golden.

这篇关于线程和设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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