Zida-Android final review-2022-6


Chapter One

1.1.3 Android architecture

  1. application layer.
  2. Application framework layer, content providers are in this layer .
  3. core class library
  4. Linux kernel layer

1.1.4 Dalvik virtual machine

1.4 Android program structure

1.5 Management and use of resources

The resources are in the src/main/res directory

Calling method:

1. In Activity, you can call image resources through the getResources().getDrawable method.

getResources().getDrawable(R.mipmap.ic_launcher);  //调用以mipmap开头的文件夹中的资源文件
getResources().getDrawable(R.drawable.icon);       //调用以drawable开头的文件夹中的资源文件

2. Call image resources in XML files

@mipmap/ic_launcher  //调用以mipmap开头的文件夹中的资源文件
@drawable/icon       //调用以drawable开头的文件夹中的资源文件

1.6.2 Use of Logcat

Divided into 6 levels

Chapter two

2.3 Common properties of interface layout

attribute name Functional description
android:id Set layout identifier
android:layout_width Set the width of the layout
android:layout_height Set the height of the layout
android:backgroud Set the background of the layout
android:layout_margin Set the distance between the current layout and the screen borders, surrounding layouts or controls
android:padding

Set the distance between the current layout and the controls in the changed layout

2.4.1 Linear layout

Two common properties in LinearLayout

android:orientation Set the order of controls within the layout
android:layout_weight Set the weight of the control in the layout, and the attribute value can directly write the int value

third chapter

3.1.1 TextView control is used to display text information.

android:layout_width

Control the width of the text box

android:layout_height

Control the height of the text box

android:text     

Display text string

android:textColor

Control text color

android:textSize

Control text size

android:gravity

Control the position of the text box, if set to 'center', it will be displayed in the center

3.1.2 EditText control edit box is a subclass of TextView control

android:layout_width

Control the width of the edit box

android:layout_height

Control the height of the edit box

android:hint

Set the prompt information displayed on the space

android:maxLines

Set the maximum number of lines in the edit box

android:textColor

Control the color of input text in the edit box

android:textSize

Control the size of input text in the edit box

android:textStyle

Control the style of input text in the edit box

3.1.3 Three methods for clicking on Button control, code

1. Specify the value of the onClick attribute in the layout file

<Button
    ......
    android:onClick="click" />

2. Use anonymous inner classes

btn.setOnClickListener(new View.OnClickListener()
    @Override
    public void onClick(View view){
    //实现点击事件的代码
    }
);

3. Use Activity to implement the OnClickListener interface

public class Activity extends AppCompatActivity implements View.onClickListener{
    @Override 
    protected void onCreate (Bundle savedInstanceState){
    ......
    btn.setOnClickListener(this);
    }   
    @Override 
    public void onClick(View view){
    //实现点击事件的代码
    }
}

3.2.2 Commonly used data adapters

BaseAdapter SimpleAdapter ArrayAdapter

Chapter Four

4.1 Activity life cycle

1. Start state 2. Running state 3. Pause state 4. Stop state 5. Destroy state

4.3 Start and close Activity code will be written

start up:

public void startActivity(Intent intent)

Intent intent = new Intent(MainActivity.this,secoundActivity.class);
startActivity(intent);

closure:

public void finish()
    
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new Voew.OnClickListener()I
    @Override
    public void onClick(View v){
    finish();    //关闭当前的activity
});

4.3.1 Intent

1. Explicit Intent: Directly specify the target component. Implementation code:

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

The first parameter this represents the current Activity , and the second parameter SecondActivity.class represents the target Activity to jump to .

2. Implicit Intent: It does not clearly indicate the target component that needs to be activated. It is widely used in different applications for message transmission. Implementation code:

<activity android:name=".SecondActivity">
    <intent-filter>
        <action android:name="cn.itcast.START_ACTIVITY"/>     
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

4.3.2 IntentFilter filter

1. Action attribute matching rules

<intent-filter>
    <action android:name"android.intent.action.DEIT"/>
    <action android:name"android.intent.action.VIEW"/>
</intent-filter>

2. Data attribute matching rules

<intent-filter>
    <data android:mimeType="video/mpeg" android:scheme="http......"/>
    <data android:mimeType="audio/mpeg" android:scheme="http......"/>
    ......
</intent-filter>

3. Category attribute matching rules

<intent-filter>
    <category android:name"android.intent.category.DEFAULT"/>
    <category android:name"android.intent.category.BROWSABLE"/>
    ......
</intent-filter>

4.4 Jump between activities

4.4.1 Data transfer between activities

(1) Use the putExtra() method of Intent to transfer data

(2) Use the Bundle class to transfer data

4.4.2 Data return between activities

  1. The startActivityForResult() method is used to start an Activity. When the opened Activity is destroyed, data will be returned from the destroyed Activity.
  2. The setResult() method is used to carry data for return.
  3. onActivityResult() method, used to receive returned data.

4.5.2 Activity startup mode

There are 4 startup modes: standard singleTop singleTask singleInstance mode, the most commonly used of which is standard 

4.6.2 Comparison of Fragment life cycle and activity unit life cycle

        The Activity life cycle has 5 states, starting state, running state, paused state, stopped state, and destruction state. The Fragment life cycle also has several states.

        Because Fragment is used embedded in Activity, all its life cycle states are directly affected by the life cycle state of the Activity to which it belongs. 1. When a Fragment is created in an Activity, the Fragment is in the starting state ; 2. When the Activity is suspended, all Fragments in it are also suspended ; 3. When an Activity is in the running state, each Fragment can be operated individually , such as adding or deleting, when the adding operation is performed, the Fragment is in the startup state ; 4. When the deletion operation is performed, the Fragment is in the destroyed state .

chapter Five

5.1 Five data storage methods:

  1. File storage
  2. SharedPreferences storage
  3. SQLite database storage
  4. Content Provide
  5. Network Storage

5.2 File Storage

1. Internal storage

FileOutPutStream fos = openFileOutPut(String name, int mode);
FileInputStream fis = openFileInput(String name);

mode indicates the operation mode of the file. There are 4 values :

  1. MODE_PRIVATE: This file can only be read and written by the current program.
  2. MODE_APPEND: The content of this file can be appended.
  3. MODE_WORLD_READABLE: The contents of the file can be read by other programs.
  4. MODE_WORLD_WRITEABLE: The contents of the file can be written by other programs.

2. External storage: dynamically apply for permissions

5.4 SQLite database storage

Concept: It can store large amounts of data in applications and manage and maintain the data.

Two important classes: SQLiteOpenHelper class, SQLiteDatabase class

5.4.3 Transactions in databases, 4 basic elements

  • Atomicity : The operations in the transaction either all succeed, or all fail and roll back.
  • Consistency : Database transactions cannot destroy the integrity of relational data and the consistency of business logic.
  • Isolation : Operations within a transaction must be blocked and will not be affected by other transactions.
  • Durability : Once a transaction is committed, the changes to the data made by the transaction are persisted in the database.

Chapter Six

6.1 Content Provider Overview

• Content Provider ( ContentProvider) is one of the four major components of the Android system. It is used to save and retrieve data . It is an interface for sharing data between different applications in the Android system .

ContentProvider (data provider) is an interface mechanism for sharing data between applications. It is a more advanced data sharing method that can specify the data that needs to be shared, while other applications can use it without knowing the source of the data. , path, query, add, delete and update the shared data.

In the Android system, many data built into the Android system are also provided to users through ContentProvider , such as address books, audio and video files, and image files.

working principle:

Data model : ContentProvider uses a simple table based on the database model to provide the data that needs to be shared. In the table, each row represents a record, and each column represents data of a specific type and meaning, and each data record contains a name. Identifies each piece of data with a field class of "_ID" .

Uri: consists of 3 parts, scheme authority path

6.4 Content Observer Concept

•If the application needs to monitor whether the data shared by ContentProvider changes in real time, it can use the content observer ( ContentObserver) provided by the Android system .

• Content Observer (ContentObserver ) is used to observe changes in the data represented by the specified Uri . When ContentObserver observes changes in the data represented by the specified Uri , it will trigger the onChange() method. At this time, use ContentResovler in the onChange() method. Changed data can be queried.

•To use ContentObserver to observe data changes, you must call ContentResolver 's notifyChange() method in ContentProvider's delete() , insert() , and update() methods.

 working principle:

 Chapter VII

7.1 Overview of the broadcast mechanism

  • Broadcast (Broadcast ) is a mechanism used to transmit messages between applications.
  • BroadcastReceiver is a type of component used to filter, receive and respond to broadcasts.
  • Broadcast receivers can monitor broadcast messages in the system and communicate between different components '

Register broadcast receiver

Dynamic registration:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver = new MyBroadcastReceiver();   //实例化广播接收者
        // 实例化过滤器并设置要过滤的广播
        String action = "android.Telephony.SMS_RECEIVED";
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(action);
        registerReceiver(receiver, intentFilter);  // 注册广播
    }

    @Override
    protected void onDestrpy(){
        super.onDestroy();
        unregisterReceiver(receiver); // 当Activity销毁时注销广播接收者
    }

 Static registration: in AndroidManifest.xml

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
</receiver>

7.3.3 Types of broadcasts

Unordered broadcast: Unordered broadcast is executed completely asynchronously. When sending a broadcast, all broadcast receivers listening to the broadcast will receive the message, but the order of reception is uncertain.

Ordered broadcast: Receive according to the priority of the receiver. Only one broadcast receiver can receive the message. After the logic execution in this broadcast receiver is completed, the message will continue to be delivered.

priority

 intercept

If you want to intercept an ordered broadcast, you must intercept the received broadcast in a higher priority broadcast receiver. Add abortBroadcast() method to intercept broadcast.

chapter eight

8.1 Overview of the service

Service is one of the four major components of Android . It is an application component that can perform operations in the background for a long time and does not provide a user interface. Service can interact with other components and is generally started by Activity , but it does not depend on Activity . When the Activity 's life cycle ends, the Service will continue to run until its own life cycle ends.

Service is usually called "backend service", where the word "backend" is relative to the front desk, specifically referring to its own operation that does not rely on the user's visible UI interface . In addition, Service also has relatively Long time running characteristics. There are two main application scenarios, namely background operation and cross-process access.

8.3 Service life cycle

Different methods are used to start services, and their life cycles are also different.

8.4 How to start the service

The startService () method starts the service. The service will run in the background for a long time, and the status of the service has nothing to do with the status of the person who started it. Even if the component that started the service has been destroyed, the service will still run.

8.5.1 The difference between local service communication and remote service communication

In the Android system, there are two ways of service communication, one is local service communication and the other is remote service communication.

Local service communication refers to communication within an application, while remote service communication refers to communication between two applications.

When using these two methods for communication, a prerequisite must be met, that is, the service must be opened in a binding manner .

Remote service communication is implemented through AIDL (Android Interface Definition Language), which is an interface definition language (Interface Definition Language). Its syntax format is very simple and is very similar to the interface defined in Java, but there are several differences, as follows :

  • The source code for an AIDL-defined interface must end in .aidl
  • Except for the basic data types, String, List, Map, and CharSequence, all other data types used in the AIDL interface need to be imported into the package, even if they are in the same package.

data transfer

In the Android system, the data transferred between processes includes:

  1. Basic data types supported by Java language
  2. User-defined data type

Chapter nine

9.1 Accessing the Internet via HTTP

URL-based request and response functions can be realized through the standard Java class HttpURLConnection .

9.1.2 Use HttpURLConnection to access the network

URL url = new URl("http://www.itcast.cn");  //在URL构造方法中传入要访问资源的路径
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");  //设置请求方式
conn.setConnectTimeout(5000);  //设置超时时间
InputStream is = conn.getInputStream();  //获取服务器返回的输入流
conn.disconnect();  //关闭http连接

Submit data using GET method

Obtain the resource information pointed to by the request URL in the form of an entity, and the parameters it submits to the server follow the request URL. The content of the network URL accessed by GET method is generally less than 1024 bytes.

Submit data using POST method

The submitted data is encapsulated in the request entity in the form of key-value pairs. The user cannot see the requested data through the browser , so the POST method is relatively safer than the GET method .

9.2 Use WbeView control for network development

9.3 JSON data

The object structure  starts with "{" and ends with "}"

The array structure starts with " [ " and ends with " ] "

It should be noted that if you use JSON to store single data (such as "abc"), you must use an array structure , not an object structure. Because the object structure must be in the form of "key name: value". Additionally, JSON files have a .json extension.

9.3.2 Parsing of JSON

Use the JSONObject class to parse object-structured JSON data

JSONObject jsonObj = new JSONObject(jo=son1);
Sreing name = jsonObj.optString("name");
int age = jsonObj.optInt("age");
boolean married = jsonObj.optBoolean("married");

 Use the JSONArray class to parse JSON data in an array structure

JSONArray jsonArray = new JSONArray(json2);
for(int i=0 ;i < jsonArray.length() ; i++){
    JSONObject jsonObj = jsonArray.getJSONObject(i);
    String name = jsonObj.optString("name");
int age = jsonObj.opyInt("age");
}

Parse object-structured JSON data using the GSON library

Gson gson = new Gson();
Person1 person = gson.fromJson(json1,Person1.class);

Parse array-structured JSON data using the GSON library

Gson gson = new Gson();
Type listType = new TypeToken<List<Person2>>(){}.getType();
List<Person2> person2 = gson.fromJson(json2,listType);

9.4 Handler message mechanism

Handler is an asynchronous callback mechanism, mainly responsible for communicating with child threads.

The Handler mechanism mainly includes four key objects:

  1. Message : Message, which is uniformly queued by MessageQueue and processed by Handler .
  2. Handler : Processor, mainly responsible for sending and processing messages .
  3. MessageQueue : Message queue, mainly used to store messages sent by Handler, and executed according to the first-in-first-out rule.
  4. Looper : Message loop, continuously extracting Message from MessageQueue and executing it.

        The Handler message mechanism is completed through the combined use of the above four key objects. Create a Handler object in the UI thread and send messages to the MessageQueue through the sendMessage() method of the object. Then call the loop() method through the Looper to continuously obtain messages from the MessageQueue and distribute them to the Handler. Finally, through the Handler's handleMessage () method processes the obtained message. 

chapter Ten

10.1 Commonly used drawing classes, functions and methods

  • The Bitmap class can obtain image file information, perform operations such as cutting, rotating, and scaling images, and can save image files in a specified format.
  • The BitmapFactory class is a bitmap factory, which is a tool class.
  • The Paint class represents a brush and is used to describe the color and style of graphics.
  • The Canvas class represents the canvas, and various graphics can be drawn through the methods provided by this class.

 Chapter 11

11.1.1 Use the MedidPlayer class to play audio 

The MediaPlayer class is used to play audio and video files . This class provides comprehensive methods to support audio files in multiple formats (3gp , mp4 )

11.1.2 Play audio  using the SoundPool class

SoundPool is an audio pool that can play multiple short audios at the same time and takes up relatively few resources . It is suitable for playing key sounds or message prompts in applications .

11.2.1 Use VideoView control to play videos

The VideoView control is used to play videos . With it, you can complete a simple video player.

 11.2.3 MediaPlayer class and SurfaceView control to play videos

MediaPlayer can play videos, but it has no image output when playing videos, so you need to use the SurfaceView component to display images. This approach is easier to extend than VideoView .

The SurfaceView component inherits from View and is a component used to display images. The biggest feature of SurfaceView is its double buffering technology. The so-called double buffering technology means that there are two threads inside it .

Guess you like

Origin blog.csdn.net/Ipkiss_Yongheng/article/details/125098767