Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Android SDK Application structure

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shares with you the content of a sample analysis of the structure of an Android SDK application. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Source

The first step

Open Eclipse and look in Package Explorer for the project we have created. In the "src" folder, you should see the project package named by the setup project. The package should contain our Activity class file, which is what you want to open in the editor. The source folder holds all the Java files we need to develop Android applications.

Every time we create a project, we create a package to hold the various Java class files. An application may have more than one package, and each package may contain multiple class files. The processing code in these class files can present our application to the user, respond to the user's interaction, and perform any necessary processing. In essence, the class file is dividing the application-related code according to the object-oriented conceptual model.

We will further discuss the concept of Java and the corresponding practice in future articles. In today's tutorial, you only need to understand that a Java application will split all kinds of processing tasks into a certain number of objects. Each object is defined by a class declaration, which is usually a separate file in an application, but can also be nested within other class files. An object is basically a large piece of code that carries part of a function related to the application. The code in the class file can reference other classes in the application or other packages in the application.

When you start developing your application, you first need to add the Java class to the package in the source folder. A typical Android application that provides a user interface will have at least one Activity file, and more Activity classes will be used for different screen displays in the application. Other types of applications, such as tools or services, adopt different structures. First of all, you will pay attention to the type of application like Activity UI, and then come into contact with other application types after you have mastered it.

Step two

Now look at the Activity class file in the new application. We will explore the Activity code further in subsequent articles in this series of tutorials, so you don't have to pay too much attention to the details for now. Today we are mainly focused on the main Activity in the application, which will take effect at the same time after the application is launched. Your application may also launch some other Activity for user interaction. When we create our own project, Eclipse will set up the application and use the main Activity as the main class-- it will also be displayed as the main Activity in the project list, as we'll see later.

In the main Activity class, you will see the onCreate method, which contains code that will be executed when the Activity is created-- that is, when the application starts. In this method, you will see the following lines of code:

SetContentView (R.layout.activity_main)

After we start the project, the contents of this line are used to specify the layout file we created and tell Android to use it as a content view. This means that whatever is contained in the layout file will be presented to the user when the Activity is displayed on the screen.

We will discuss this topic further later, and for the time being we need to focus on the "R.layout.activity_main" syntax. This is how our Java code references application resources. We will use a similar syntax to reference resources through their ID values, such as pictures and data values. The "R" represents the application resource, and the latter part is used to specify the type of entry stored in the "res/layout" directory-- in this case, the layout. These resources are eventually identified by their names-- for the layout in the example, the file name is used. From this, it is inferred that the syntax we are going to use is "R.type.name". After we start programming, you will start to use this syntax.

In future articles in this series, we will add code to the Activity class file for user interaction. Now open the "res" folder in the application and you will find multiple subfolders in it. These folders are created by default by Eclipse and ADT after we enable the new Android project, but we may need to add other directories for different types of resources.

two。 Layout resources

As we have seen, the layout files generated after the project is created are saved in the "res/layout" folder. If an application has multiple Activity screens, it is common to keep a separate layout file for each screen. You may also use layout files for individual UI entries. When you create class files for Activity, you need to use setContentView for layout settings as described above. In addition, you can also use Java code to set up the layout-this is an alternative. In our example, the layout settings are generated dynamically when the application is executed. However, the advantage of using XML is that we can directly feel the visual effect of the layout scheme in the interface design work.

In the application's main layout file (which should be open with an editor by now), you will see the XML structure. If you haven't been exposed to XML before, don't worry, we'll discuss these basics further in a later article. For now, all you need to know is that XML is a markup language, similar to HTML-- if you've been exposed to Web development before. XML files use a tree structure as a data model. Typically, a layout file has a root layout element and uses it as a specific layout type model-- which contains child elements for UI entries, including buttons, pictures, text, and so on.

3. Paintable resources

You should see multiple folders with the word "drawable" in their names under the resources directory, which are used to hold the picture files used by the application. These image files can be digital picture files that we have prepared outside of Eclipse in formats such as PNG or JPEG. Alternatively, you can define specific paintable resources by describing shapes, colors, and appearance through XML code. Once we have created the file in the drawable folder, we can reference it in the application layout file or in the Java code. In this way, the previously prepared visual elements can be used to apply UI.

Drawable folders for each density bucket are retained in the resource directory. These density buckets are the general classification basis of pixel density for all kinds of devices running Android system. The specific categories can be divided into low, medium, high, ultra-high and ultra-high density. By choosing from the corresponding types, we can easily simplify the support process of multi-screen density with the help of density buckets. This means that when we include picture files in our project, we can place them in folders of different densities and provide versions that meet various density schemes through cropping.

4. Data resources

In the "res" directory, we will see some folders with the word "values" in the title. These folders are used to hold the data values that you want to use in the application. These values can contain text strings as well as numbers. The values folder that contains the XML file lists one or more of the values. Each list contains a name and a value in the content. Other files in the application, such as Java classes or layout files, can refer to these values by these names. In a typical use case, we can need to display content-- such as buttons-- in a UI element with these values stored in a text string.

Different value files in the application allow you to modify values for a specific screen size and API level. If the same value is sufficient for multi-Sichuan devices, it can be saved directly in the "Values" folder.

5. List

When you look at the home folder in the application, you are sure to find the manifest file for the project. You can open it with an editor by double-clicking it. Next, we will see a graphical interface that displays its contents. Click the "AndroidManifest.xml" tab at the bottom of the editor window to view its XML code. This file defines all aspects of the application as a unified whole. Eclipse and ADT will create specific elements in the list as we create the application, depending on how you set up the project creation process. You can manually add other elements to the list, such as adding other Activity.

We will run some of the main elements to understand the role of the list, but there are many other elements that can be included. Among the new application project elements listed in the listing, we will see the uses-sdk element, which we use to represent the minimum and target API levels. The Application element contains attributes that point to the startup mechanism and the application name. There is also an activity element in the application element that will be launched as the primary Activity with the intent-filter element when the application starts running. When we add a new Activity to the application, a new activity element is added for each related element.

You may also need to add other elements to the list, including the uses-permission element, which lists the permissions required by the application-the list that users will see before installing the application. Permissions contain a variety of action entries, such as obtaining data over the Internet, writing to storage, or accessing other functions on the device, such as cameras. The manifest also lists the types of devices that the application can support, as well as other application components (such as background services).

6. Other documents

At this point, we have talked about the major aspects of the Android application project structure that you need to understand. As you learn about Android development, you will often deal with this content in the future. Through Eclipse, we will also see some other files and directories included in the project, but for now they can be ignored directly.

As you can see earlier, you can use "R." Syntax implements resource references. Both Eclipse and the ADT of the management system refer to resources from Java in the application. When you add or edit these resources in the project, Eclipse will write the corresponding content to the "R.java" file to help us take advantage of "R." Make a resource reference. When you start working on your own Java files, you will see Eclipse pop-up messages when referencing-a mechanism that simplifies the management of application resources. the "R.java" file is saved in the "gen" folder. Please note: do not try to edit this file directly, it will be generated automatically when we edit project resources. The system manages this process by assigning a unique integer ID to each resource in the application.

Tip: when you start to try Android application development, you may encounter problems using R. If Eclipse displays any R-related error messages, especially "R cannot be resolved to a variable", you need to check the beginning of the class file to see if there is an import statement for "R", such as "import android.R;". If you find the corresponding content, especially after the code has been copied and pasted into the file, delete this import statement. If you encounter other R-related prompts, make sure there are no errors in the resource file. If the problem persists, try using "Project", "Clean" to clean up the project. When all efforts fail, try restarting Eclipse.

Thank you for reading! This is the end of this article on "sample Analysis of Android SDK Application structure". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report