Introduction to Android Basic Development (i)

Source: Internet
Author: User

Preface: I studied C # for more than a year (from learning programming, also no two years, I am now a sophomore), the middle has been learning WP development, although the technology is not good, very slag, but Microsoft announced at the Build conference strategy let me feel it necessary to learn about Android development. About Microsoft's strategy, nothing to say, only the platform is strong, developers can survive, for Microsoft and. NET developers, it's a matter of honor and ruin. At present, WP developers may be a little hurt inside, but in personal opinion this is nothing, we still have to learn a technology to enrich themselves better. I will continue to study while studying Android. NET aspect of the technology, for Java, sophomore last semester on this course, the content of the study is very simple, and at that time also did not learn, now seems to have to fill up, take advantage of the sophomore just have an android so good learning.

The Android development environment has been set up before, and now it's time to look directly at the architecture of the Android application and how it works.

File system architecture for 1.Android Engineering

Src:java the directory where the source code resides. In the SRC folder, several packages can be created to classify the Java source program (. java file)

Gen: Automatically generated directory The Gen directory holds all files that are automatically generated by the Andriod development tool. The most important thing in the directory is the R.java file (gen\ package name \r.java). The development tool automatically modifies the R.java file according to the resources you put into the Res directory. Because the R.java file is generated automatically by the development tool, you should avoid manually modifying the R.java. R.java in the application of the role of the dictionary, it contains a variety of resource IDs, through the R.java, the application can easily find the corresponding resources.

Assets: Resource Directory, andriod in addition to providing/res directory storage resource files, in the/assets directory can also hold resource files, and the/assets directory of resource files are no longer r.java automatically generated ID, so read/ Files in the Assets directory must specify the path to the file, and the resource files in the assets are not compiled, packaged directly into the app, and the assets folder supports subdirectories of any depth.

Bin: Used to store the compiled application

Libs: Store the third-party jar used by the application.

Res: Resource (Resource) directory in this directory you can find the various resources that the application uses. such as XML interface files, pictures or data. The resource files within the Res folder will eventually be packaged into a compiled. java file, and the Res folder does not support the deep subdirectory.

Res/layout: Store a layout file with an. xml extension, and each layout file corresponds to an activity.

Res/values:strings.xml is the most important file in this folder, which holds the property values of the control objects in the layout file, and is related to the internationalization of the application.

Res/drawable: Picture Folder

Res/raw: Storing audio resource files, etc.

Andriodmanifest.xml: Project manifest file This file lists the features that the application provides, and the various components that are developed (Activity, ContentProvider, broadcast, Service) need to be configured in that file. If your app uses a built-in app (such as telephony, Internet service, SMS, GPS service, and so on), you'll also need to declare usage rights in that file. (read by the operating system when installing)

2 . Compilation Results

. java--". class--". dx--". dex--packaging (signature)--" apk

The compilation results are placed under the Bin folder in the project root directory

Bin/classes: Storing compiled Java class files

Bin/classes.dex: Storing executables created based on the compiled Java class

BIN/RESOURCES.AP_: Resources to store applications

BIN/XXX.APK: The actual Android app. apk file is actually a zip archive that contains. Dex, a compiled version of the resource (RESOURCES.ARSC), other non-compiled resources, and andriodmanifest.xml files.

3.andriodmanifest.xml File Details

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.dialogdemo"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application></Manifest>

The package property in the root node <manifest> indicates the application packages for the manifest file, Android:versionname is the version name or number of the application, and can be any appropriate value for Android: Versioncode must be an integer that indicates the version number of the application. Depending on this feature, the system can determine if the application is an update of another version.
<uses-sdk>: Indicates which version of the SDK the current application builds on.

<APPLICATION>: Defines the details of the application that corresponds to the current profile.

<activity>: equivalent to a form within Windows programming. Android:name represents the class that implements activity, and Android:label represents the name of the activity to display. The <intent-filter> child element is used to describe how the current activity is invoked. It's inside.
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>

These two parameters display our application on the launch list of the mobile application, indicating the entry of the application, and should be aware that they can only appear in an activity, which is the main activity of the application.

There are some elements in this manifest file that do not appear, but are often used in the future:

<uses-permission>: Indicates the permissions required for the application to function properly.
<permission>: Claims permissions for activities and services that represent the permissions that other applications must have to use the data or logic of the current application.
<instrumentation>: Represents code that needs to be called in critical system events.
<uses-library>: Introduction of optional Android components, such as map services.

4.Android Application Life cycle

Unlike most traditional application platforms, Android applications cannot control their own life cycles. Application components must listen for changes in the state of the application and react appropriately, and be prepared to be terminated at any time. By default, each Android application runs through its own process, and each process runs in a separate Dalvik instance. The memory and process management of each application is handled exclusively by the runtime.

Android proactively manages its resources, and it takes any action to ensure a stable and smooth user experience. This means that processes (and their applications) are sometimes terminated without warning, freeing up resources for high-priority applications, when necessary.

Active Process: An application process that is interacting with the user.

Visible process: A process that resides "visible" activity.

Start the service process: the serviced process that was started.

Background process: An activity process that is not visible and does not have any running service.

Empty process: The application's life cycle has ended, but still remains in memory of the process. Android maintains this cache to reduce startup time when the application starts again.

5. A simple Android app

First new Android Application project, locate the generated. java file in the top-level of the project's SRC folder, and add the following code:  

 Public classMainactivityextendsActionbaractivityImplementsview.onclicklistener{Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); BTN=NewButton ( This); Btn.setonclicklistener ( This);        UpdateTime ();    Setcontentview (BTN); }    Private voidUpdateTime () {//TODO auto-generated Method StubBtn.settext (NewDate (). toString ()); } @Override Public voidOnClick (View arg0) {updatetime (); }}

The OnCreate method in this class is automatically called by the system when the application is started, and its incoming parameter is savedinstancestate, and the type is Bundle,bundle is a data parameter, typically used for data transfer between the activity. In this method, the first thing to do is to associate with the superclass to complete the initialization of the entire activity. It then creates a button instance, sets the event listener for the button, calls the UpdateTime () method, and finally sets the button to the content view.
The final effect of this program is that there is only one button in the page that shows the time that it was last pressed (if it was not pressed to show when the application started).

We know that Android separates the user interface and resources from the business logic and uses XML files for interface layout, while complex business logic is given to Java code, so we seldom use the method above to build our application. Next we'll change the app above.

Res/layout/activity_main.xml:

   < Button    xmlns:android = "Http://schemas.android.com/apk/res/android"         android:id= "@+id/btn"        android:layout_width= "Fill_parent"         Android:layout_height = "Fill_parent"         android:text= ""/>

Xmlns:android= "Http://schemas.android.com/apk/res/android" This can only appear in the top-level element of the layout file, while the top-level element is generally a layout container. We have only one button element here that is placed directly in the button.

The Android:id property specifies a unique identifier for the button, which is required when we get the elements in the layout file in the. java file.

Android:text indicates the text displayed on the button.

Android:layout_width and Android:layout_height indicate the relationship between the width and height of the button and the parent element, in which case the button occupies the entire screen.

. java files:

 Public classMainactivityextendsActionbaractivityImplementsview.onclicklistener{Button btn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);             Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener ( This);    UpdateTime (); }    Private voidUpdateTime () {//TODO auto-generated Method StubBtn.settext (NewDate (). toString ()); } @Override Public voidOnClick (View arg0) {updatetime (); }}

Setcontentview (R.layout.activity_main); This is the same as the instance of the child class (or button) that we passed into a view before. All resources in the layout can be accessed by R.layout plus the name of the layout file; For example: R.layout.activity_main access is Res/layout/activity_main.xml

btn= (Button) Findviewbyid (R.ID.BTN);//Findviewbyid method to get the Button element identified as @+id/btn in the layout file

The effect is the same as the original.

We've learned some of the basics of Android development, including the file system architecture, the results of the compilation, the manifest manifest file, the lifecycle of the app, and a simple Android app. The feeling is still relatively chaotic, because it is a novice, the error is not enough please give us a lot of advice.

Before a head into C #, with the VS is really comfortable, now with eclipse is not used to, feel that the android is really more complicated than WP, come on!

Introduction to Android Basic Development (i)

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.