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

发布于 2024-07-26 00:18:38 字数 94 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

因为看清所以看轻 2024-08-02 00:18:38

对于 Android 程序而言,没有 main()
操作系统运行一个 UI 循环,它调用您在程序中定义或重写的方法。 这些方法可能是在 onCreate()onStart()onResume()onReStart() 中调用或定义的code>、onPause()onStop()onDestroy()。 所有这些方法都可以在您的程序中被覆盖。

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

Activity 生命周期 最好地描述了这一点(您的程序是一个或更多的活动,将活动视为屏幕)。

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

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.

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).

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.

心凉怎暖 2024-08-02 00:18:38

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

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

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.

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

她比我温柔 2024-08-02 00:18:38

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

应用程序类必须支持该应用程序的每个活动的方法
支持。 一般情况下,onCreate 可能相当于 main/top
功能满足您的需求。

According to:
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.

橘虞初梦 2024-08-02 00:18:38

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

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

魂牵梦绕锁你心扉 2024-08-02 00:18:38

上面的答案提供了 Android 上没有“主循环”的“原因”(理解这一点很重要)。 相反,我将为隐含的问题提供一个解决方案,因为这里的许多访问者都会寻找这个答案。

我相信在这里要做的适当的事情是创建一个作为“主循环”运行的 AsyncTask。 或者更好的是,将主循环设计为作为 java.util.concurrent future 运行,它可以在生命周期事件(如轮换!)期间启动和结束,使用信号发送(将数据分开)。 AsyncTask API 已被弃用,因为它很复杂,正确处理它相当于编写代码,这些代码将有效地作为 AsyncTask 运行,在下一个有问题的生命周期事件发生时进行清理。

请记住,这将是一个独立于 UI 的线程,因此需要快速响应 UI 线程事件,例如“onPause”和“onDestroy”。 如果您的应用程序在特定时间段(~5 秒,iirc)内没有响应这些事件或用户输入事件,它将被操作系统终止。 对于实时应用程序来说,即使在最低端设备上,也能够在 1 秒内完全响应这些事件,这确实是谨慎的做法。 您可以使用同步原语来通知其他线程它们的响应正在挂起,并且它们可以使用它们在完成时发出信号(或者在未来的情况下简单地结束)。

The above answers provide a "why" as to there's no "main loop" on Android (which is important to understand). I'll offer a solution to the implied question, instead, as many visitors here will be looking for exactly that.

I believe the appropriate thing to do, here, would be to create an AsyncTask which operates as your "main loop". Or better yet, design your main loop to run as a java.util.concurrent future, which can be started and ended during lifecycle events (like rotation!), using signaling (keep your data separate). The AsyncTask API is deprecated, because it was complex, and handling it properly amounted to writing code that would, effectively, operate as an AsyncTask which cleaned up when the next problematic lifecycle event transpired.

Keep in mind that this will be a separate thread from the UI, and, as such, will be required to respond in short order to UI thread events, like "onPause" and "onDestroy". If your app does not respond within a certain period of time (~5 secs, iirc) to these events, or user input events, it will be killed by the OS. It's really prudent, for a real-time app, to be able to fully respond to these events in under 1 sec, even on the lowest-end device. You can use synchronization primitives to notify other threads that their response is pending, and they can use them to signal when they are finished (or simply end, in the case of a future).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文