Android (Java) Learning Note 158:java Program entry and Android APK entry

Source: Internet
Author: User

Entrance to the 1.Java program:Static Main () method

 Public class extends Activity {        @Override        publicvoid  onCreate (Bundle savedinstancestate) {             Super . OnCreate (savedinstancestate);            Setcontentview (R.layout.main);        }                 Public static void  Main (string[]  args) {            System.out.println ("Hello Dalvik");}        }

2.APK Running entry:Activitythread class ( APK Entry Class is Activitythread,activity is just a callback class )

Activitythread class : It manages the execution of the main thread of the application process (equivalent to the main entry function of a normal Java program ), And according to the requirements of AMS (through the Iapplicationthread interface, AMS for client, Activitythread.applicationthread for server) is responsible for dispatching and executing activities, Broadcasts and other operations.

In an Android system, by default, individual components within an application (such as activity, Broadcastreceiver, Service) are executed in the same process , and the The main thread is responsible for the execution.

In an Android system, you can also have specific components run in different processes if they are specifically specified (via android:process). Regardless of which process the component runs in, by default they are responsible for the main thread of the process.

The main thread handles both the UI events of the activity component and the service backend services, which are often not busy. To resolve this problem, the main thread can create multiple child threads to handle the background service work, while itself concentrating on the events of the UI screen.

The primary responsibility of the "Main thread":

• quickly handle UI events . And only it handles UI events, other threads cannot access objects on the UI screen (such as TextView, etc.), and the main thread is called the UI thread. Basically, Android wants the UI thread to respond quickly to the user's requirements, and if the UI line thread too much time to handle the background work, when UI events occur, the user waits longer than 5 seconds without processing , and the Android system shows the user ANR Prompt Information .

only the UI thread can execute the OnDraw () function of the view-derived class .

• quickly process broadcast messages . The main thread handles broadcast messages in addition to handling UI events. Therefore, in Broadcastreceiver's OnReceive () function, it should not take too long, otherwise the "main thread" cannot handle other broadcast messages or UI events. If it takes longer than 10 seconds , the Android system will show the user the ANR alert message .

Precautions:

• Try to avoid having the "main thread" perform time-consuming operations so that it can quickly handle UI events and broadcast messages.

Broadcastreceiver Subclasses are stateless, that is, each time it is started, its object is created, and then its onreceive () function is called, and the object is immediately deleted when the OnReceive () function is executed. Because a new object is recreated each time its function is called, the property values in the object cannot be shared by the functions.

Additional notes:

The view component is executed by the UI thread (the main thread). If you need to quickly update the UI screen or UI drawing takes a long time, you need to use Surfaceview. It can be performed by a background thread (background thread), and view can only be performed by the UI (primary) thread. The surfaceview has an efficient rendering mechanism that lets background threads quickly refresh surface content.

View---> UI (Master) Threads

Surfaceview---> background Threads

Android (Java) Learning Note 158:java Program entry and Android APK entry

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.