Android Studio source code learning record

Posted by sriphp on Sat, 15 Jan 2022 02:31:01 +0100

1, HashMap < string, string >

* HashMap: it is a Map interface implementation based on hash table.
* the hash table is used to ensure the uniqueness of keys.
 * HashMap<String,String>
* key: String
* value: String

package cn.itcast_02;

import java.util.HashMap;
import java.util.Set;

public class HashMapDemo {
    public static void main(String[] args) {
        // Create collection object
        HashMap<String, String> hm = new HashMap<String, String>();

        // Create and add elements
        // String key1 = "it001";
        // String value1 = "Ma Yun";
        // hm.put(key1, value1);

        hm.put("it001", "Jack Ma");
        hm.put("it003", "pony ");
        hm.put("it004", "Steve Jobs");
        hm.put("it005", "Chao Yang Zhang");
        hm.put("it002", "Qiu Bojun"); // wps
        hm.put("it001", "Bill Gates");

        // ergodic
        Set<String> set = hm.keySet();
        for (String key : set) { //Syntax explanation: traverse the data of type String key in the set container one by one
            String value = hm.get(key);
            System.out.println(key + "---" + value);


it004 --- jobs
it003 --- Ma Huateng
it005 --- Zhang Chaoyang
it002 --- Qiu Bojun
it001 --- Bill Gates

The above is taken from: Collection framework note 54: a case of HashMap set (HashMap < string, string >) of Map set

I understand: HashMap is used to store data (. put(key, data)) and take out data (. get(key)) with the key value key as the index

2, addTextChangedListener()

Function: monitor text change status; After adding TextWatcher, the TextWatcher methods will be called whenever the text of TextView changes.

TextWatcher's method:

editText.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              //This method is called to indicate that in the s string, the count characters starting from the start position will be replaced by the new text with the length of after. If you change s in this method, an error will be reported.

            public void onTextChanged(CharSequence s, int start, int before, int count) {
              //This method is called to indicate that in the s string, the count characters starting from the start position have just replaced the old text with a length of before. If you change s in this method, an error will be reported.

            public void afterTextChanged(Editable s) {
              //If this method is called, it indicates that somewhere in the s string has been changed.

reference material:

3, registerReceiver

this.mContext.registerReceiver(mDemoMsgReceiver, new IntentFilter(OTGService.OTG_ACTION_DISCONNECTED_DEMO))

registerReceiver: register a broadcast receiver; Receive the broadcast information specified by IntentFilter through mDemoMsgReceiver - check whether the OTG service has issued the instruction. If so, execute the operation in the broadcast receiver.

4, ProgressBar (progress bar)

ProgressBar: where time-consuming operations are required (when the user logs in, the background is sending a request and waiting for the server to return information), add a progress bar to let the user know that the current program is executing, or intuitively tell the user the execution progress of the current task, etc.

ProgressBar.setVisibility(View.GONE)-Set the performance of the progress bar display: visible,invisible and gone. Not specified android:visibility Controls are visible when. invisible Indicates that the control is invisible, but it still occupies the original position and size. It can be understood that the control becomes transparent. gone Indicates that the control is not only invisible, but no longer occupies any screen space.

5, Runnable

Easy to use:

    // Step 1: directly create thread helper objects through anonymous classes, that is, instantiate thread helper classes
    Runnable mt = new Runnable() {
                    // Step 2: copy run() to define thread behavior
                    public void run() {

                // Step 3: create a thread object, that is, instantiate a thread class; Thread class = thread class;
                Thread mt1 = new Thread(mt, "Window 1");
                // Step 4: control the status of the thread through the thread object, such as run, sleep, suspend / stop

6, synchronized (an important concept in thread synchronization)

In Java, the synchronized keyword is used to control thread synchronization, that is, in a multithreaded environment, to control that the synchronized code segment is not executed by multiple threads at the same time.

synchronized is a keyword in Java. It is a kind of synchronous lock. It modifies the following objects:
1. Modify a code block. The modified code block is called synchronization statement block. Its scope of action is the code enclosed in braces {}, and the object of action is the object calling the code block;
2. Modify a method. The modified method is called synchronous method. Its scope of action is the whole method, and the object of action is the object calling the method;
3. Modify a static method. Its scope of action is the whole static method, and the object of action is all objects of this class;
4. Modify a class. Its scope of action is the part enclosed in parentheses after synchronized. The main object of action is all objects of this class.

reference material: Usage of Synchronized in Java 


        1. When two concurrent threads (thread1 and thread2) access the synchronized code block in the same object (syncThread), only one thread can be executed at the same time, and the other thread is blocked. You must wait for the current thread to execute the code block before executing the code block.

        2.synchronized only locks objects, and each object has only one lock associated with it. The above code is equivalent to the following code

        3. When one thread accesses a synchronized(this) synchronized code block of an object, another thread can still access the non synchronized(this) synchronized code block in the object.

        4. grammar

//When there is an explicit object as a lock, you can write the program in a way similar to the following.

public void method3(SomeObject obj) {
//obj locked object
    synchronized(obj) {
// todo
//When there is no explicit object as a lock and you just want to synchronize a piece of code, you can create a special object to act as a lock:

class Test implements Runnable {
        private byte[] lock = new byte[0]; // Special instance variable
        public void method() {
            synchronized(lock) {
// todo synchronization code block

        public void run() {


Synchronized acts on the writing of the entire method.  

1 public synchronized void method() {
2 // todo
3 }
1 public void method() {
2     synchronized(this) {
3 // todo
4     }
5 }

7, Message of Handler

android.os.Message defines a message, contains the necessary description and attribute data, and this Object can be sent to Android os. Handler processing. Attribute fields: arg1, arg2, what, obj, replyTo, etc; Arg1 and arg2 are used to store integer data; What is used to save the message identifier; Obj is any Object of type Object; replyTo is a message manager and is associated with a handler, which processes the messages in it. Usually, the message Object is not directly new. Just call the obtainMessage method in the handler to directly obtain the message Object.

handle. The removecallbacks method deletes the specified Runnable object and stops the thread object

Author: Hou egg_
Source: Jianshu

8, String Usage Summary

1. String.split(String regex): separate strings with regrex as the boundary

public class Split {
    public static void main(String[] args) {

        String[] aa = "aaa|bbb|ccc".split("\\|");

        for (int i = 0 ; i <aa.length ; i++ ) {





2. regionMatches of string

regionMatches(int firstStart, String other, int otherStart, int len)
When a string calls this method, it means to take a substring with length len from the firstStart position of the current string; Then, starting from the otherStart position of another string, take a substring with a length of len, and then compare whether the two substrings are the same. If the two substrings are the same, return true, otherwise return false.

Topics: Android android-studio