1. From system architecture to Hello World

Source: Internet
Author: User

Android origin and development:

The Android operating system was originally developed by Andy Rubin in 2003 and mainly supports mobile phones. August 2005 was funded by Google's takeover. In November 2007, Google developed an open mobile alliance with 84 hardware manufacturers, software developers and telecoms operators to develop an improved Android system. Google then released the Android source code in the form of an Apache open source license. The first Android smartphone was released in October 2008. Android is gradually expanding into tablets and other areas such as TVs, digital cameras, game consoles, and more. In the first quarter of 2011, Android's market share in the world surpassed the Symbian system for the first time in the world. In the fourth quarter of 2013, the global market share of Android handsets has reached 78.1%. September 24, 2013 Google developed the operating system Android in the 5-year-old birthday, the world's adoption of the system's number of devices has reached 1 billion units.

Android's system architecture:

  Mainly divided into four-tier architecture and five sections

Linux kernel layer: This layer primarily provides the underlying driver for Android hardware. such as display driver, audio driver, camera driver, Bluetooth driver, WiFi driver, power management and so on.

System run-level: This layer provides some feature support through C + +. such as SQLite provides database support, opengl| El provides support for 3D drawings, the WebKit library provides support for the browser kernel, and so on.

Application Framework Layer: This layer provides the API support that developers may use when developing applications. Some of the core apps that Android comes with are also done using these APIs.

Application layer: Almost all programs installed on the phone belong to this layer. Includes SMS, contact features, programs that have been downloaded in the App Store, and programs that you've developed yourself.

Android's four components:

Activity: The façade of all applications, whatever is seen in the app, is stored in the activity

Service: Not visible, can run silently in the background. Even if the application exits, it can still run.

Broadcast receiver: The running app receives messages from various places (such as phone calls, text messages). You can also send messages to other places through this component.

Content Provider: Provides the possibility to share data between applications.

Hello World:

  Here's the first Hello World program to start the Android route.

The steps for setting up the environment include the following three steps:

1. Installing the JDK

· 2. Go to the Android Developer website to download the development tools that bind Eclipse, SDK, SDT.

3. After extracting the files downloaded in 2, you need to click on the SDK Manager management tool, download the relevant other version of the Android SDK, the downloaded files will be automatically placed in the SDK folder.

After all three steps have been completed and you open Eclipse, you will find an Android-related icon added to the Eclipse's toolbar:

Images from left to right are the SDK manager and the icon that launches the Android emulator. We click on the second icon to create an Android emulator and start the effect after launch

At this point, you can open the device window in eclipse, you can see the running equipment situation, of course, you can switch to the DDMS view, you can clearly see the device-related more detailed situation.

So much has been said to begin the development of the first Hello World program:

Direct new Android Application then go all the way next and select the blank activity when you need to select the activity type:

  

In fact, Hello World is done. Run Android application and then select the Android emulator you just created as the running device, and the resulting interface is as follows:

  

It feels kind of cool! Let's analyze the composition of the Code:

  

Can see inside there are many catalogs, instantly feel dumbfounded. But don't worry, actually we need to focus on a few categories:

First, the bin, Lib directory can skip directly, which is usually stored in the jar package. In general, the bin directory is the program at compile time automatically generated files and the current program compiled can be directly installed APK package, the Lib directory is generally stored in the introduction of third-party packages.

The SRC directory generally makes our source code

There is a R.java file in the Gen directory, where all the resources we add to the project can be found in the corresponding ID. It is recommended that you do not manually modify the file.

Assets directory, this directory with a few, generally used to store files that need to be packaged with the program, in the program can dynamically read the contents. If you use WebView to load a local Web page in your program, all Web page related files also need to be placed in this directory.

Res directory, there are a bit more files in this directory. Simply speaking, all the pictures, layout files, strings and other resource files used in the program should be in this directory. In fact, the automatically generated code in R.java is based on the resources in this file generated. Of course, these resource files also have a certain set of rules: than the film is generally placed in the drawable sub-directory, layout sub-directory is generally stored in the configuration file, the values sub-directory generally stored string files.

Androidmanifest.xml: The core configuration file for the entire project. The configuration of the four components, Application permissions additions, minimum and compatible versions of the specified program must be done here.

Project.Properties: This file is very simple, just the SDK version of the specified project compilation.

Explore how Android programs are associated from Androidmanifest.xml:

The source code for the Androidmanifest.xml file is as follows:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.helloworld"4 Android:versioncode= "1"5 Android:versionname= "1.0" >6 7     <USES-SDK8         android:minsdkversion= " a"9 android:targetsdkversion= "+" />Ten  One     <Application A         Android:allowbackup= "true" - Android:icon= "@drawable/ic_launcher" - Android:label= "@string/app_name" the Android:theme= "@style/apptheme" > -         <Activity -             Android:name= "Com.example.helloworld.MainActivity" - Android:label= "@string/app_name" > +             <Intent-filter> -                 <ActionAndroid:name= "Android.intent.action.MAIN" /> +  A                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> at             </Intent-filter> -         </Activity> -     </Application> -  - </Manifest>
Androidmanifest.xml

You can see that an activity is registered inside, and we find the relevant code for the activity:

1  PackageCom.example.helloworld;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.view.Menu;6 7  Public classMainactivityextendsActivity {8 9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -     } -  the @Override -      Public BooleanOncreateoptionsmenu (Menu menu) { -         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); +         return true; -     } +  A}
Mainactivity.xml

As mentioned earlier, the activity is the façade of all applications, then how to find out how to run the Activiy when the program runs depends on the two words in the intent-filter tag in Androidmanifest.xml.

Continue looking at Mainactivity's code to see that all Android activity must inherit from the activity class. There is a OnCreate method in the activity class that must be executed during activity creation.

Through Setcontentview (R.layout.activity_main) in mainactivity; To specify the layout that the activity displays. This layout actually corresponds to the previous activity_main.xml in the Res/layout directory.

The source code for this file is as follows:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context=". Mainactivity " >Ten  One     <TextView A         Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "@string/hello_world" /> the  - </Relativelayout>
Activity_main.xml

You can see a textview tag in the layout file, this is a control that displays text for Android, where the control's Android:text property specifies the text content that is displayed. We do not recommend the use of hard-coded strings for internationalization and take-up considerations. So you can see that the specified control in the file displays a value of @string/hello_world. This actually indicates that this string comes from a string resource file: Hello_world in/values/strings.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4     <stringname= "App_name">Hello World</string>5     <stringname= "Action_settings">Settings</string>6     <stringname= "Hello_world">Hello world!</string>7 8 </Resources>
Strings.xml

You can actually find references to string resources in Androidmanifest.xml as well.

Well, at this point finally understand the whole program is the whole kind of association run. In a nutshell, you must have a Androidmanifest.xml file that configures information about the activity of the entire project, and the activity and related resource files referenced in the configuration file need to be configured under the RES path. Referenced by: If it is in a Java file like R.string.hello_world, and in XML it is referenced in the form of a @string/hello_world. Finally need to explain the next res directory in the beginning of the drawable directory, these sub-files actually correspond to different rates, the purpose is to be compatible with more devices, but sometimes the artist may only provide a version of the picture, then all put in the drawable-hdpi directory.

Two little skills to transition to the next stage of learning:

1. Feel the first program to run the interface a little clown, it is important because there is a title bar, in fact, as long as the OnCreate method of the Setcontentview method call before adding a code can be removed this effect beautiful title bar:

1 @Override 2     protected void onCreate (Bundle savedinstancestate) {3         Super . OnCreate (savedinstancestate); 4         requestwindowfeature (window.feature_no_title); 5         Setcontentview (r.layout.activity_main); 6     }
onCreate

The results of the changes are as follows:

  

2. Enable Logcat. This name definitely has a certain fondness for Tomcat. Or you should use cat to keep a diary. The Android logging tool feels super easy to use and features huge power.

The primary log levels are as follows:

Verbose: A log that corresponds to the lowest level trivial type.

Other levels from low to High are: Debug--->info--->warn--->error.

While the use of Android log is simple and rough, the method of logging is static method, the name of the method to take the first letter of the different log level (exactly the same). The first parameter passed in is tag, and the second one is the specific log information. eg

LOG.V ("HelloWorld", "This is verbose log of mainactivity!");

Of course, Android also has a very convenient way to print out the call stack information at the row log wtf (what the fAck?) ), this method feels unusually powerful.

If you can only print the log, but also can not say to the subsequent debugging of the code how much effect, but eclipse inside is integrated with a very cow Android log viewing artifact, is the previous said to open the Logcat window:

Here you can customize the filters, filter by level, and so on. Viewing the log later can be more targeted.

OK, the first step of the Android road is over!

   

1. From system architecture to Hello World

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.