Android entry (1), Android entry (

Source: Internet
Author: User

Android entry (1), Android entry (

Link: http://www.orlion.ga/387/

I. Android system architecture

1,

 

    1. Linux kernel layer, which provides underlying drivers for Android devices

    2. The system Runtime Library layer provides main features for the Android system through some C/C ++ libraries. For example, the SQLite Library provides database support, the OpenGL | EL Library provides 3D drawing support, and the Webkit Library provides browser kernel support. In addition, the android Runtime Library mainly provides some core libraries that allow developers to develop android applications using java. In addition, the Android Runtime Library also contains the Dalvik virtual machine, which enables every android application to run in an independent process and has its own Dalvik Virtual Machine instance.

    3. Application Framework layer, which mainly provides APIs that may be used to build an App

    4. Application Layer. All applications installed on mobile phones belong to this layer.

 

What Android provides:

    1. Four components: Activity, Service, Broadcast Receiver, and Content Provider. Activity is the facade of all Android applications, and everything seen in the application is put in Activity. The Service has been running silently in the background. Even if the user exits the application, the Service can continue to run. The Broadcast Receiver can allow applications to receive Broadcast messages, such as phone calls and text messages. Of course, their own applications can also send Broadcast messages. Content Provider provides the possibility to share data between applications. For example, to read contacts in the system phone book, you must use the Content Provider.

    2. The Android system provides developers with a variety of system controls, allowing us to easily compile beautiful interfaces. Of course, if you have a high taste and are not satisfied with the built-in control effect, you can also customize your own control.

    3. SQLite database, Android system also comes with this lightweight embedded relational database with fast computing speed, not only supports standard SQL, but also supports operations through Android encapsulated APIs

    4. Location, Android basically has built-in GPS

    5. Powerful multimedia. Android provides a wide range of multimedia services, such as music, video, recording, photo taking, and alarm. These services can be controlled by code in the APP.

    6. Many mobile phones have built-in sensors, such as direction sensors and acceleration sensors.

 

2. HelloWorld, the first Android Project

1. Create an android project

File-> new-> Android Application Project

Next.

2. Run the project

First, create an android simulator and click Android Virtual Device Manager in the eclipse toolbar.

Right-click the project and choose run as> Android Application.

 

3. project directory introduction:

A. src: Where java code is stored

B. gen: The automatically generated content mainly contains an R. java file. Any resource added anywhere in the project will generate a corresponding resource id in it. Do not modify this file.

C. assets: stores files packaged with the program. In addition, if WebView is used in the program to load local webpages, all webpage-related files are stored in this directory.

D. bin: mainly contains files automatically generated during compilation. One of the files contains the apk installation package compiled by the current project.

E. libs: if a third-party jar package is used in the project, you need to put the jar package under the libs directory, and the jar package under this directory will be automatically added to the build path.

F. res: All images, la S, strings, and other resources used in the project are stored in this directory. java content is automatically generated according to the files in this directory. There are many subdirectories in this directory. The images are placed under the drawable directory, the layout is placed under the layout directory, and the strings are placed under the values directory.

G. AndroidMainfest. xml: This is the configuration file of the entire project. All the four components defined in the program must be registered in this file. You can also add a permission statement to the App in this file. You can also specify the minimum compatible version and target version of the program when building a project.

H. project. properties: This file specifies the SDK version used to compile the program using a line of code.

 

Iii. How to Run HelloWorld

Open the AndroidMainfest. xml file and find the following code:

<activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

This code indicates registration of the MainActivity, which is not registered in AndroidMainfest. xml. The two lines of code in intent-filter are very important. <action android: name = "android. intent. action. MAIN "/> and <category android: name =" android. intent. category. LAUNCHER "/> indicates that MainActivity is the main Activity of the project. Click the application icon on the mobile phone to start the Activity first.

So what is the use of MainActivity? Activity is the facade of the Android application. Everything seen in the application is placed in the Activity. Therefore, the interface for running the program is MainActivity. MainActivity. java code:

package ga.orlion.helloworld;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}

MainActivity is inherited from Activity, and Activity is a base class provided by Android system. All activities in our project must inherit from it to possess Activity features, then we can see that there are three methods in MainActivity. onCreateOptionsMenu () is used to create a menu. We can ignore it first, mainly look at the onCreate () method, onCreate () A method is a method that must be executed when an activity is created. The design of the Android Program focuses on the separation of logic and view. It is not recommended to directly write the interface in the Activity. Generally, the interface is written in the layout file and then introduced in the Activity. The second line of the onCreate () method calls the setContentView () method, which introduces an activity_main layout to the current Activity. The layout files are all defined in the res/layout directory. Open activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="ga.orlion.helloworld.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" /></RelativeLayout>

Here we need to note the HelloWorld we see on the program interface! Strings are defined separately in res/values/string. xml rather than in this file,

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">HelloWorld</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string></resources>

The layout file activity_main.xml is referenced by the @ string/hello_world method.

 

Res directoryAll Folders starting with drawable store images, all folders starting with values store strings, and layout folders store layout files, the menu folder is used to store menu files. So many drawable folders are used to make programs compatible with more devices. When creating a program, it is best to provide several copies of different resolutions for an image in these folders, when the program is running, the file under the folder is automatically loaded based on the current device resolution. If there is only one resolution, put it in the drawable-hdpi folder.

In the code, you can use R. string. hello_world to obtain the string reference. In xml, you can use @ string/hello_world to obtain the string reference.

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.