java Android 中是否有类似于 C/C++ 中包含程序主循环的“int main”的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1099640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:10:58  来源:igfitidea点击:

Is there a function in Android analogous to "int main" in C/C++ which contains the program's main loop?

javaandroidactivity-lifecycle

提问by RyanCheu

Normally in a C or C++ program there's a main loop/function, usually int main (). Is there a similar function that I can use in android Java development?

通常在 C 或 C++ 程序中有一个主循环/函数,通常是int main (). 我可以在android Java开发中使用类似的功能吗?

回答by Will

As far as an Android program is concerned there is no main(). There is a UI loop that the OS runs that makes calls to methods you define or override in your program. These methods are likely called from/defined in onCreate(), onStart(), onResume(), onReStart(), onPause(), onStop(), or onDestroy(). All these methods may be overriden in your program.

就 Android 程序而言,没有 main()。操作系统运行的 UI 循环调用您在程序中定义或覆盖的方法。这些方法可能在 onCreate()、onStart()、onResume()、onReStart()、onPause()、onStop() 或 onDestroy() 中调用/定义。所有这些方法都可能在您的程序中被覆盖。

The fundamental issue is that the OS is designed to run in a resource constrained environment. Your program needs to be prepared to be halted and even completely stopped whenever the OS needs more memory (this is a multitasking OS). In order to handle that your program needs to have some of all of the functions listed above.

根本问题是操作系统旨在在资源受限的环境中运行。您的程序需要准备好在操作系统需要更多内存时停止甚至完全停止(这是一个多任务操作系统)。为了处理您的程序需要具有上面列出的所有功能中的一些。

The Activity lifecycle describes this best (your program is one or more Activities, think of an Activity as a screen):

Activity 生命周期对此进行了最好的描述(您的程序是一个或多个 Activity,将 Activity 视为一个屏幕):

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Bottom line: Your program 'starts' at onCreate() through onResume() but the OS is running the loop. Your program provides callbacks to the OS to handle whatever the OS sends to it. If you put a long loop at any point in your program it will appear to freeze because the OS (specifically the UI thread) is unable to get a slice of time. Use a thread for long loops.

底线:您的程序通过 onResume() 在 onCreate() 处“启动”,但操作系统正在运行循环。您的程序向操作系统提供回调以处理操作系统发送给它的任何内容。如果您在程序中的任何一点放置一个长循环,它似乎会冻结,因为操作系统(特别是 UI 线程)无法获得一部分时间。使用线程进行长循环。

回答by weilin8

In Android environment, there is no main(). The OS relies on the manifest file to find out the entry point, an activity in most case, into your application.

在Android环境中,没有main()。操作系统依赖清单文件来找出应用程序的入口点,在大多数情况下是一个活动。

You should read http://developer.android.com/guide/topics/fundamentals.htmlfor more detail.

您应该阅读http://developer.android.com/guide/topics/fundamentals.html了解更多详情。

回答by drudru

According to: http://developer.android.com/guide/tutorials/hello-world.html

根据:http: //developer.android.com/guide/tutorials/hello-world.html

The application class must support a method for each activity that the Application supports. In the general case, the onCreate is probably equivalent to the main/top function for your needs.

应用程序类必须支持应用程序支持的每个活动的方法。在一般情况下, onCreate 可能相当于您需要的 main/top 函数。

回答by Cambesa

Maybe it's possible by creating a timer and execute custom functions at every tick, reset the timer when it's at a specific time

也许可以通过创建一个计时器并在每次滴答时执行自定义函数,在特定时间重置计时器