What is a. r file?
As shown, we can find our TextView element through the Findviewbyid method through the incoming r.id.show, the Findviewbyid method is also very well understood, from the view through the ID found, then what is R?
The use of resource files is divided into code that is used in the resource file and referenced in other resource files. When we compile an Android app, Android automatically generates an R class that generates the corresponding inner class based on different resource types, which contains all the resource files used in the system, and in the Android Studio compilation environment, the R file is "D:\ Usersfiles\androidstudioprojects\myfirstapp\app\build\generated\source\r\debug\com\example\boyliupan\ Myfirstapp "directory (this project), where" D:\UsersFiles\AndroidStudioProjects "is the path to my own defined workspace," Myfirstapp "is the name of the project I created.
In the network materials and books of inquiry
Tip The R.java file is stored in the "/ge/<package name>" directory, but I did not find it in the compilation results of Android studio.
Let's open the R.java and see what's Inside:
The 第1-6 line is a system-generated note that is automatically generated by the AAPT tool through the resource data it finds, and it cannot be manually modified (because you do not have to modify it, and each rebuild will be overwritten).
AAPT is the Android Asset Packaging tool, the Android resource pack, which is usually called automatically by the IDE, and we don't need to use it manually.
第8-39 line is the AAPT automatically generated code, the name of the class is R, which defines a number of constants. We can think of R.java as a resource dictionary that records the IDs of all the resources that can be used.
With these IDs, we can easily use these resources in our programs.
The rules for generating R.java files for the AAPT tool are mainly the following two articles:
1. Each class of resource pairs corresponds to an inner class of the R class. For example, all string resources correspond to a string inner class, and all layout class resources correspond to the interior classes of layouts.
2. Each specific resource to a field that corresponds to a public static final int of the inner class, such as the identifier of show we have defined in the example, we can find it through r.id.show.
Second, res directory description
The Res directory contains all the resources used by Android apps, including picture resources, string resources, color resources, and more. It contains multiple directories (only part of it):
A). Where the drawable folder is used to store. png,. 9.png,. JPG and other picture resources (. 9.png is an Android-specific image format that can be stretched to a non-deforming effect), their rate of change is high to low, and if you plan to include an image or icon in your Android app, you should put them in this directory.
B) The layout folder holds the application's layouts files, which are stored as XML.
C) Values This directory also contains some XML files, but is primarily the key-value pair to be referenced in the application. These XML files declare arrays, colors (color), measures (Dimension), and strings. The reason for putting these things separately in separate XML files is to consider that these values can be used in a multilanguage environment without changing the source code. For example, depending on the language of the user, information in different applications can be in multiple language versions.
The relationship between resources and R.java in res directory
Developers just edit the Res directory resources, R.java based on the RES content is fully automatically generated, each resource is assigned an ID, programming can be called by ID, below we will say how to use.
We know that the use of resources is divided into Java code and referenced in the XML file, here are two scenarios to illustrate how to use:
1. Using in Java code
This is very simple, we have used in the previous example, call R.id directly. The resource ID can be called to (the resource under the identifier is shown here, and if it is a string resource , it is r.string. Resource ID, please understand it yourself)
2. Referencing in an XML file
Reference in the XML file, we only need to follow the "@< resource corresponding to the internal class class name >/< resource Item name >" To access, such as a string resource by default defined a App_name resource we can call "@string/app_ Name
3. Exceptions
When we use identifiers in XML, these identifiers do not need to be defined using specialized resources, and we use the "@id/< identifier code >" To assign identifiers directly.
Access to Directory resources:
The Res directory resource is accessed through the appropriate methods of resources.
The relevant information can be obtained in this way:
Iii. Description of Androidmanifest.xml
Manifest file: This XML file contains meta-information from Android apps and is an important file in every Android project. When the software is installed, the four components of Android (Activity, ContentProvider, Broadcastreceiver, Service) need to be in the file, And a list of user rights required to run the Android app, such as phone calls, text messages, the Internet, access to the SD card, and a detailed description of the project structure of the Android app. You can include the following information by:
1. The package name of the application, which will be the unique identity of the app.
2. the components contained in the application, such as activity,services and so on.
3. Application-compatible minimum version
4. permission declarations required by the application to use the system
5. permission statements required by other programs to access the app
Here is the manifest file for our example, the gray font is the description:
Iv. description and use of the application authority
Android applications want to invoke the functionality of certain systems must have the appropriate permissions, such as we want to call the GPS module, we must have access to the use of GPS, our application may also be called by other applications, it is also necessary to declare the need to call our application permissions.
1. The permissions required to run the app
Only need to <manifest in the manifest file: Add <uses-permission.../> child elements to the/> element. For example:
2. What permissions are required to invoke our app
The Declaration method is consistent with the above, except that the declared position is placed under a specific component element node.
The Android SDK provides a number of permissions, which are located in the Manifest.permission class, and we commonly use the following permissions:
|
Permissions |
Description |
| Access_network_state |
Allow applications to get network status |
| Access_wifi_state |
Run the app to get WiFi status |
| Battery_stats |
Allow applications to get battery status |
| BLUETOOTH |
Allow applications to connect to Bluetooth |
| Bluetooth_admin |
Allow applications to discover Bluetooth |
| Broadcast_sms |
Allow app broadcasts to receive SMS alerts |
| Call_phone |
Allow applications to use dial-up functionality |
| CAMERA |
Allow applications to use the camera |
| Change_network_state |
Allow applications to change network status |
| Change_wifi_state |
Allow app to change WiFi status |
| Delete_cache_files |
Allow applications to delete cache files |
| Flashlight |
Allow applications to use flash |
| INTERNET |
Allow applications to open network sockets |
| Modify_audio_settings |
Allow applications to modify global sound settings |
| Process_outgoing_calls |
Allow applications to listen, control, cancel outgoing calls |
| Delete_packages |
Allow applications to remove installation packages |
| Read_contacts |
Allow applications to read contacts |
| Read_history_bookmarks |
Allow applications to read historical bookmarks |
| Read_owner_data |
Allow applications to read user data |
| Read_phone_state |
Allow applications to read phone status |
| Read_phone_sms |
Allow applications to read text messages |
| REBOOT |
Allow the app to restart the phone |
| Receive_mms |
Allow applications to receive, monitor, and process MMS messages |
| Receive_sms |
Allow applications to receive, monitor, and process text messages |
| Record_audio |
Allow applications to use audio recordings |
| Send_sms |
Allow apps to send text messages |
| Set_orientation |
Allow applications to rotate the screen |
| Set_time |
Allow application to set time |
| Set_time_zone |
Allow the application to set the time zone |
| Set_wallpaper |
Allow app to set wallpaper |
| Vibrate |
Allows the application to control the vibrator |
| Write_contacts |
Allow applications to write to contacts |
| Write_history_bookmarks |
Allow applications to write historical bookmarks |
| Write_owner_data |
Allow applications to write user data |
| Write_sms |
Allow applications to write text messages |
| 。。。。 |
。。。。。 |
table taken from "Crazy Android Handout" follow-up will do to complement
V. Conclusion & NEXT Announcement
Recently bought a "crazy Android Handout" The third edition, read the first few chapters, I have to say, the author is very thoughtful, the content of the arrangement, a lot of content is easy to understand, read a deep feeling, this series of articles for reading notes, there will be some content in reference books, I think the more practical to write the place will be summed up, there will be borrowed part, to the author salute, I hope that the students who want to learn Android development can buy a learning learning.
The next article will briefly introduce the components commonly used in Android development, the concept of things, after the introduction of the new chapter will begin the development of Android in the various UI components learning.
Android Development Self-study note (Android Studio1.3.1) -3.android Application Structure analysis