Introduction to Android Development (2) | Understanding Android Application Components

Insert picture description here

Android open source mobile operating system

Android (Android) is a free and open source operating system based on the Linux kernel. It is mainly used in mobile devices, such as smartphones and tablets. It is led and developed by Google and the Open Mobile Alliance. The Android operating system was originally developed by Andy Rubin and mainly supports mobile phones. In August 2005, it was acquired and injected by Google. In November 2007, Google and 84 hardware manufacturers, software developers and telecom operators formed the Open Mobile Alliance to jointly develop and improve the Android system. Subsequently, Google released the source code of Android under the authorization of Apache license.

Insert picture description here
1. Android provides a rich application framework that supports the development of innovative applications and games for mobile devices in the Java language environment:

2. The application provides multiple entry points

Android applications are built by combining various components that can be called individually. For example, a component can be a single "Activity" that provides a screen for the user interface, or a "service" that performs work independently in the background. You can use Intent to start another component from one component. You can even start components in different applications, for example, start Activity in a map application to display addresses. This mode can provide multiple entry points for a single application and enable any application to handle operations that other applications may call like the user's "default setting".

3. The application can be adapted to different devices

Android provides an adaptive application framework that you can use to provide unique resources for different device configurations. For example, you can create different XML layout files for different screen sizes, and the system will determine the layout to be applied according to the current device's screen size. If any application functions require specific hardware such as cameras, the availability of the device functions can be queried during runtime. If necessary, you can also declare the necessary functions of your application so that application markets such as the Google Play Store cannot install your application on devices that do not support these functions.

Android basics

Android applications using the Java programming language, the Android SDKtool code you write - along with any data and resource files compiled into an APK package, a APK file that contains all the contents of Android applications, it is a device based on the Android system used Install application files.

  • The Android operating system is a multi-user Linux system, in which each application is a different user;
  • By default, the system assigns a unique Linux user ID to each application (this ID is only used by the system and is not known to the application). The system sets permissions for all files in the application, so that only the user ID assigned to the application can access these files;
  • Each process has its own virtual machine (VM), so the application code runs in an environment isolated from other applications;
  • By default, each application runs in its own Linux process. Android starts the process when it needs to execute any application component, and then closes the process when it is no longer needed or the system must restore memory for other applications.

App Components

Today we come to understand the basic building price of Android applications. Application components are the basic building blocks of Android applications. Each component is a different point through which the system can enter your application. Not all components are actual entry points for users. Some components depend on each other, but each component exists as an independent entity and plays a specific role—each component is a unique building block that helps define the overall application behavior.

Android applications are mainly composed of the following four main components:

  • Activities: They determine the user interface and the smartphone screen that handles user interaction.
  • Services: They handle background processing related to the application.
  • Broadcast Receivers: They handle the communication between the Android OS and the application.
  • Content Providers: They deal with data and database management issues.

Activities

ActivityIt denotes a screen with a user interface Briefly Activityperforming an operation on the screen. For example, an email application might have one Activitythat displays a list of new emails, another Activitycomposes an email, and another Activityreads the email. If there are multiple applications Activity, one of them should be marked as displayed when the application starts Activity.

One Activityas Activitya subclass of the class is implemented as follows:

public class MainActivity extends Activity {
    
    
}

Services

Services Serviceare components that run in the background and can perform long-running operations. Services have no user interface. For example, Service may play music in the background when the user is in a different application, or may obtain data through the network without preventing the user from Activityinteracting with it. Other components such as Activity can start the service, let it run or bind to it to interact with it.
ServiceIs Service类implemented as a subclass of:

public class MyService extends Service {
    
    
}

Broadcast Receivers

The broadcast receiver only responds to broadcast messages from other applications or systems, and is a component used to respond to system-wide broadcast notifications. For example, an application can also initiate a broadcast to let other applications know that some data has been downloaded to the device and is available for it to use, so this is the broadcast receiver that will intercept this communication and initiate appropriate actions

Broadcast receivers are BroadcastReceiverimplemented as subclasses, and each broadcast is Intentdelivered as an object.

public class MyReceiver  extends  BroadcastReceiver {
    
    
   public void onReceive(context,intent){
    
    }
}

Content Providers

The content provider component provides data to an application upon request, (that is, manages a set of shared application data, other applications can query the data through the content provider, and even modify the data (if the content provider allows), therefore, anyone with appropriate permissions Any application can query a certain part of the content provider (such as ContactsContract.Data) to read and write information about a specific person). These requests are handled by methods of the ContentResolver class. Data can be stored in the file system, database or other places.

The content provider is also suitable for reading and writing private data that your application does not share. The content provider is implemented as a subclass of the ContentProvider class and must implement a set of standard APIs to enable other applications to perform transactions.

public class MyContentProvider extends  ContentProvider {
    
    
   public void onCreate(){
    
    }
}

ManifestFile manifest file

The main task of the manifest file is to inform the system about application components

Insert picture description here

ManifestFile function:

  • Declaring components 声明组件
    Before the Android system starts the application component, the system must confirm the existence of the component by reading the AndroidManifest.xml file ("manifest" file) of the application. Your application must declare all its components in this file, which must be located in the root directory of the application project directory.

  • Determine any user permissions required by the application, such as Internet access or read access to user contacts

  • According to the API used by the application, declare the minimum API level required by the application

  • Declare hardware and software features used or required by the app, such as cameras, Bluetooth services, or multi-touch screens

  • The API library that the application needs to link to (except the Android framework API), such as the Google Maps API library

  • Other functions

Other components

The android engineering project will also use other components when building the above entities:

  • Fragments: Represents a part of the user interface in the Activity.
  • Views: UI elements drawn on the screen, including buttons, list forms, etc.
  • Layouts: View the hierarchical structure that controls the screen format and view appearance.
  • Intents: Messages connect components together.
  • Resources: External elements, such as strings, constants and drawable pictures.
  • Manifest: The configuration file of the application.

Application resources

In addition to coding, an Android application needs other separate resources to build a good experience application, such as images, colors, layout definitions, audio files, and any content related to the visual presentation of the application. Generally located res/in each sub-resource folder under the folder directory, the res/ directory contains different resources in different sub-directories:

Insert picture description here

  • drawable/: The image is compiled into a bitmap. .png, .jpg, .gif or XML files, status lists, graphics, and animated image files that can be drawn. They are stored in res/drawable/ and can be accessed from the R.drawable class

  • layout/: An XML file that defines the layout of the user interface. They are stored in res/layout/ and can be accessed from the R.layout class

  • mipmap: mipmap is used to put the launcher icon

  • values/: An XML file containing simple values ​​such as strings, integers and colors. For example, here are some file name convention resources that can be created in this directory:

Reference: https://blog.csdn.net/anxpp/article/details/50809160?utm_source=app

Guess you like

Origin blog.csdn.net/weixin_43853746/article/details/108564240