2013-06-27 22 views

回答

0

您应该了解UI线程的概念。基本上,有一个主线程(称为您的回调方法的活动),您可以启动其他线程。你开始的线程不能更新UI,只是UI线程。

查看更多在这里:What is the Android UiThread (UI thread)

4

我beleive你可能已经阅读What is an Activity?文档之前。如果没有,那么请做。在这里你可以阅读更多关于Android的process and threads。现在,回答你的问题:

活动是一个独立的线程?

每个活动都不是一个独立的线程。正如评论中提到的@ android.h,所有活动都运行在相同的UI thread上。

多个活动可以同时运行多线程应用程序吗?

如上所述,所有Activities,Services,ContentProviders,BroadcastReceivers等都在UI线程上运行。话虽如此,你可以从一个活动本身开始多个线程。因此,您的应用程序可以使用多个线程,但运行多个活动并不会使其成为多线程。

考虑到多个活动,您可能会阅读Tasks and Back Stack文档。它突出了关于多种活动的概念:

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes。所以这里是多个活动的工作原理。

希望这可以让你的概念更加清晰。

4

那么活动是一个独立的线程吗?

是和否。一个具有一个Activity的Android应用程序将具有单个进程和单个线程,但是如果有多个应用程序组件,它们通常会使用相同的线程(除了某些使用自己的线程执行工作的Android类)。

请阅读以下...

Processes and Threads

如果这样可以将多个活动同时运行是一个多线程应用程序?

仅当Activity完全可见时才被视为“正在运行”。例如,当弹出窗口(例如对话框等)出现时,底层Activity仍然部分可见,但将处于“暂停”状态。如果另一个Activity已启动并完全隐藏了前一个(无论它是您自己的应用程序还是外部应用程序的一部分),则以前的Activity将进入“停止”状态,甚至可能被销毁。

基本上,Android Activity不是一个允许在多线程环境中执行多任务的工作。 Activity基本上是一个UI框架,用于提供按钮,文本视图,图像等,并允许用户进行交互。

也见...

Application Fundamentals

...也看看这里的Activity生命周期图...

Activity Lifecycle

0

AS android.h提到的所有UI对象,活动在主线程上运行。但仍然可以使用异步任务从另一个线程中的Web服务读取数据,这将帮助您不要在后台执行任务。 希望我的回答对你有所帮助。

0

活动性是用户界面,而线程是执行某些代码的进程工作者。 Android有一个控制所有用户界面(UI)的主线程,所以如果你没有指定一个运行你的活动的不同线程,主线程将运行你的整个UI。

0

活动是用户在使用应用程序时看到的任何UI,线程是您的任务正在运行的地方..总是有一个线程称为主线程来运行应用程序中的所有UI和进程,如果您想要提高执行速度以创建更多线程,以便主线程不会受到干扰,而且您的繁重任务将在后台运行。

相关问题