繁体   English   中英

Android应用程序上的活动和线程有什么区别?

[英]what is the difference between activity and thread on android app?

我对Android应用程序上的活动和线程之间的区别感到困惑。 像独立线程这样的活动是什么? 如果是这样,多线程应用程序中的多个活动可以同时运行吗?

谢谢

我相信你可能已经阅读了什么是活动的文档 之前。 如果没有,那么请做。 在这里你可以阅读更多关于android中的进程和线程 现在,回答你的问题:

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

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

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

如上所述,所有活动,服务,ContentProviders,BroadcastReceivers等都在UI线程上运行。 话虽这么说,您可以从活动本身开始多个线程。 因此,您的应用程序可以使用多个线程,但运行多个活动不会使其多线程。

关于多个活动,您可能会阅读“ 任务”和“后台堆栈”文档。 它突出了多个活动的概念:

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 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 所以这是多个活动的工作原理。

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

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

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

请阅读以下内容......

进程和线程

如果可以,多个活动同时作为多线程应用程序运行?

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

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

也可以看看...

应用基础知识

...还可以在这里查看Activity生命周期图...

活动生命周期

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

在这里查看更多: 什么是Android UiThread(UI线程)

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

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

Activity是用户在使用应用程序时看到的任何ui,一个线程是你的任务正在运行的地方..如果你想增加它,总会有一个被称为主线程的线程来运行你的所有ui和应用程序中的进程你需要创建更多线程的执行速度,这样你的主线程就不会受到干扰,你的繁重任务就会在后台运行。

Activity表示单个屏幕,它有UI,其他手线程没有UI,用于执行后台操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM