在Android的主回路 [英] Main loop in Android

查看:137
本文介绍了在Android的主回路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一般pretty的对Java,但我有一点的C和C ++的经验。通常在C或C ++程序中有一个主循环/函数,通常INT主要()。是否有类似的功能,我可以在Android的Java开发使用?

I'm pretty new to java in general, but I have a bit of experience in C and C++. 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?

推荐答案

据一个Android程序而言不存在的main()。有该操作系统的运行,使得调用方法定义的UI环或覆盖在你的程序。这些方法有可能从一个名为/中定义的onCreate(),ONSTART(),onResume(),onReStart(),在onPause()的onStop(),或的onDestroy()。所有这些方法都可以被覆盖在你的程序。

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.

最根本的问题是,OS是专为在资源受限的环境中运行。你的程序需要ppared被叫停,每当操作系统需要更多的内存(这是一个多任务操作系统),甚至完全停止了$ P $。为了处理您的程序需要有一些上述所有的功能。

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

<一个href="http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle">http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

底线:你的程序的开始'在的onCreate()通过onResume(),但操作系统正在运行的循环。你的程序提供了回调操作系统来处理任何操作系统发送给它。如果你把一个长的循环在你的程序中任何地方它会出现冻结,因为操作系统(特别是UI线程)是无法得到的时间片。使用一个线程长期循环。

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.

这篇关于在Android的主回路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆