Basic Development for Android (1): basic development for android

Source: Internet
Author: User

Basic Development for Android (1): basic development for android

I have been learning C # for more than a year (I haven't been learning programming for two years now), and I have been learning WP development in the middle, although the technology is a waste of resources, the strategy announced by Microsoft at the Build Conference made me feel necessary to learn about Android development. There is nothing to say about Microsoft's strategy. Only when the platform is powerful can developers survive. For Microsoft and. NET developers, this is a matter of prosperity and destruction. At present, WP developers may feel a little hurt, but in my opinion, this is nothing. We should learn another technology to enrich ourselves. I will continue to learn about Android. NET technology, for Java, sophomore year last semester this course, the content is very simple, and did not learn how to, now it seems to have to make up, take a look at the course of Android in your sophomore year.

 

The android development environment has been set up before. Now let's take a look at the Android Application structure and running principles.

1. File System Structure of the Android Project

Src: directory where java source code is stored. You can create several packages in the src folder to classify and store Java source programs (. java files)

Gen: automatically generates all files automatically generated by the Andriod development tool in the gen directory. The most important part in the directory is the R. java file (gen \ package name \ R. java ). The development tool automatically updates and modifies the R. java file based on the resources in the res directory. Because the R. java file is automatically generated by the development tool, manual modification to R. java should be avoided. R. java plays a dictionary role in the application. It contains the IDs of various resources. Through R. java, the application can easily find the corresponding resources.

Assets: Resource Directory. In addition to providing the/res directory to store resource files, Andriod can also store resource files in the/assets directory, and the resource files under the/assets Directory will not be R. java automatically generates an id. Therefore, the path of the file must be specified to read the files in the/assets Directory. The resource files in assets are not compiled and directly packaged into the application, the assets folder supports subdirectories in any depth.

Bin: used to store compiled applications

Libs: Third-Party JAR used by applications.

Res: the Resource Directory contains various resources used by the application. Such as xml interface files, images, or data. The resource files in the res folder are eventually packaged into the compiled. java file. The res Folder does not support deep subdirectories.

Res/layout: stores the layout file named. xml. Each layout file corresponds to an Activity.

Res/values: strings. xml is the most important file in this folder. It stores the property values of control objects in the layout file and is related to the internationalization of applications.

Res/drawable: Image folder

Res/raw: stores audio resource files, etc.

AndriodManifest. xml: the project list file lists the functions provided by the application. The developed components (Activity, ContentProvider, Broadcast, and Service) must be configured in this file, if the application uses built-in applications (such as telephone services, Internet services, SMS services, and GPS services), you must declare the permission to use the application in this file. (Read by the operating system during installation)

 

2. Compilation result

.Java--developer.class--developer.dx--demo.dex--package ()--》.apk

Put the compilation result in the bin folder under the root directory of the project.

Bin/classes: stores compiled Java class files

Bin/classes. dex: stores executable files created based on compiled Java classes.

Bin/resources. ap _: stores application resources.

Bin/XXX.apk: the actual Android Application .. The apk file is actually a ZIP compressed file, including. dex, resources. arsc, other uncompiled resources, and the AndriodManifest. xml file.

 

3. Detailed description of the AndriodManifest. xml file

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.dialogdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <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>    </application></manifest>

The package attribute in the root node <manifest> specifies the application package corresponding to the configuration file. android: versionName is the version name or number of the application, and can be any suitable value. android: versionCode must be an integer indicating the application version number. The system can determine whether the application is updated in another version based on this feature.
<Uses-sdk>: indicates the SDK version of the current application.

<Application>: defines the details of the application corresponding to the current description file.

<Activity>: it is equivalent to a Form in Windows programming ). Android: name indicates the class implementing the Activity, android: label indicates the name of the Activity used for display. <Intent-filter> A child element is used to describe the conditions in which the current Activity is called. The
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>

These two parameters display our applications on the mobile app startup list, indicating the app portal. When using these parameters, note that they can only appear in one Activity, this Activity is the main Activity of the application.

There are still some elements that do not appear in this configuration file but will be frequently used in the future:

<Uses-permission>: indicates the permissions required for normal application running.
<Permission>: Declares permissions for activities and services, indicating that other applications must have permissions to use the data or logic of the current application.
<Instrumentation>: indicates the code to be called in key system events.
<Uses-library>: introduces optional Android components, such as the map service.

 

4. Android Application Lifecycle

Unlike most traditional application platforms, Android applications cannot control their own lifecycles. The application component must listen for changes in the application status and make appropriate responses, and be prepared to be terminated at any time. By default, each Android application runs through its own process, and each process runs in an independent Dalvik instance. The memory and process management of each application are specially processed by the runtime.

Android will take the initiative to manage its resources, and it will take any measures to ensure stable and smooth user experience. This means that, when necessary, processes (and their Applications) are sometimes terminated without warning, releasing resources for high-priority applications.

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

Visible process: processes that reside in "visible" Activity.

Start a Service process: a Service process that has been started.

Background process: invisible, and there is no running Service Activity process.

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

 

5. A simple Android Application

First, create an Android Application Project, find the generated. java file in the innermost layer of the src folder corresponding to the Project, and add the following code:

public class MainActivity extends ActionBarActivity implements View.OnClickListener{    Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                btn=new Button(this);        btn.setOnClickListener(this);        updateTime();        setContentView(btn);    }    private void updateTime() {        // TODO Auto-generated method stub        btn.setText(new Date().toString());    }    @Override    public void onClick(View arg0) {        updateTime();    }}

The OnCreate method in this class will be automatically called by the system when the application starts. The input parameter is savedInstanceState, the type is Bundle, and the Bundle is a data parameter, it is generally used for data transmission between activities. In this method, the first thing to do is to associate with the superclass to complete the initialization of the entire Activity. Then create a button instance, set the event listener for the button, call the updateTime () method, and set the button as the content view.
The final result of this program is that there is only one button in the page to display the last time you press it (if not, the start time of the application is displayed ).

We know that Android separates user interfaces and resources from business logic and uses XML files for interface layout, while complex business logic is handed over to java code for completion, therefore, we rarely use the above method to build our applications. Next, let's change the above application.

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" can only appear in the top-level elements of the layout file, while the top-level elements are generally a layout container. Here we only have one Button element and put it directly in the Button.

Android: The id attribute specifies a unique identifier for the Button. This attribute is required when we obtain 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 this example, the buttons occupy the entire screen.

 

. Java file:

public class MainActivity extends ActionBarActivity implements View.OnClickListener{    Button btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);             btn=(Button)findViewById(R.id.btn);        btn.setOnClickListener(this);        updateTime();    }    private void updateTime() {        // TODO Auto-generated method stub        btn.setText(new Date().toString());    }    @Override    public void onClick(View arg0) {        updateTime();    }}

SetContentView (R. layout. activity_main); this is the same as the instance where we passed in a View subclass (that is, the Button. All resources in the layout can be accessed by adding the layout file name to R. layout. For example, R. layout. activity_main accesses res/layout/activity_main.xml.

Btn = (Button) findViewById (R. id. btn); // use the findViewById method to obtain the Button element marked as @ + id/btn in the layout File

The running effect is the same as that of the original one.

Now I have learned some basic knowledge about Android development, including the file system structure of the Android project, the compilation result, the Manifest list file, the application lifecycle, and a simple explanation of the Android application. I feel that the writing is still quite messy. As a newbie, please give me more advice on errors.

Previously, it was really comfortable to use VS, but now I am not used to using Eclipse. I feel that android is actually much more complicated to write than WP. Come on!

 

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.