Android 设计思想
2010-06-17 03:29:00 来源:WEB开发网做大量的事,放到线程中去
如果你的程序需要执行高CPU占用或长时间的计算,你需要将它挪到线程中。这可以防止显示给用户”程序没反应”的对话框,最终的结果是,你的程序将让出系统的使用权。
默认的情况下,所有的代码是运行在同一个线程中的。处理UI就是在同一个线程中。比如,当用户按下一个按键,一个key-down事件被添加到主线程队列中。事件处理处理器必须出列,然后快速的处理事件。如果不是这样的话,在几秒中后系统推断程序正在挂起中,并提示用户去关掉它。
如果你有长时间运行的代码,并运行在事件处理线程中,将会阻塞事件处理器。这使处理被延时,并会导致”程序为反应”对话框的出现。为了避免这样,需要把它移动程序中;[点击这里学习如何使用]
Avoid Huge Activities
Any application worth using will probably have several different screens. When partitioning your UI, be sure to make effective use of Activities.
Depending on your development background, you may interpret an Activity as similar to something like a Java Applet, in that it is the entry point for your application. However, that’s not quite accurate: where an Applet subclass is the single entry point for a Java Applet, an Activity should be thought of as one of potentially several entry points to your application. The only difference between your “main” Activity and any others you might have is that the “main” one just happens to be the only one that expressed an interest in the “android.intent.action.MAIN” action in your AndroidManifest..xml file.
So, when designing your application, think of your application as a federation of Activities. This will make your code a lot more maintainable in the long run, and as a nice side effect also plays nicely with Android’s application history and “backstack” model.
更多精彩
赞助商链接