android foundation interface development considerations

Keywords: Android network xml

When doing Android development, we must pay attention to that the main thread cannot change the UI interface. If there is a crash when the program is running, and if there is no obvious syntax error, please check whether there is a conflict or crash in your own process. If there is a connection with the background, that is, when the request is sent to the server, you need to pay special attention, or there is no error, but the code of network request will not be executed. In this case, if there is no problem with the set parameters or other places, but the code of network connection is not executed, you need to check whether there is a conflict between your processes at this time.

In general, I will instantiate a Thread class in the main Thread, open another Thread, and then operate in it, and use handle to pass the value after getting the data. The specific usage is as follows:

//Define a handler for data transfer between threads. If you define a global variable, it has limitations. Because the process is loaded asynchronously, it often appears. When the page displays data, no data has been captured. Therefore, it is recommended to use the handler for data transfer

private Handler myHandler =new Handler(){
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                   .....
                   break;
            }
        }
    };

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        
        ......
        
        new Thread(new Runnable() {
            @Override
            public void run() {
            ......
            Message msg = new Message();
            msg.what = 1;
            msg.obj = ...(Passed value);
            myHandler.sendMesage(msg);
            
        //If there is no value to be passed, but the response operation needs to be performed according to the status, sendEmptyMessageDelayed() can be used directly; the first parameter is equivalent to msg.what, which is used to judge and select the status. The second parameter is delay time, which is how long the statement is delayed in execution (milliseconds).
        myHandler.sendEmptyMessageDelayed(1,0);
        
        
        //If you change the UI interface directly in the process and there is no response, try to wrap it with loop. Prepare(); loop. Loop(); before and after the statement, for example:
        Looper.prepare();
        Toast.makeText(getContext,"Data acquisition successful",Toast.LENGTH_SHORT).show();
        Looper.loop();
        
            }
        //If the process does not respond, first check whether the process start is set, that is, write. start(); start when instantiating the class
        }).start();
    }

android input box prompt text Click to disappear

Add the property android:hint = "prompt text" to the input box.

android center text in TexiView

android:gravity="center";

Activity? XML view mode does not display and error prompt

Error:Error: 'B' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

This kind of error prompt mainly refers to the file name in the project, which does not conform to the system's naming specification. Like my error prompt, there should be no uppercase letters in this file name, but I have the uppercase letter "B", so an error is reported, which causes the view mode of the activity ﹐ XML file to not display normally

Posted by josh_mcqueen on Mon, 30 Mar 2020 10:34:23 -0700