Android development road--1

1. After Android 9 network requests need to load the adaptation file:

network_security_config.xml:

<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" overridePins="true" />
            <certificates src="user" overridePins="true" />
        </trust-anchors>
    </base-config>
</network-security-config>

Configure in Application in the application configuration file .xml:

android:networkSecurityConfig="@xml/network_security_config"

Just load the configuration file

2. To solve the problem of Handler memory overflow, create a static class that inherits Handler, use WeakReference weak reference to load the activity, and use various functions of the handler in it

//防止Handler内存溢出 创建静态继承Handler子类
    static class MyHandler extends Handler {
        private WeakReference<MainActivity> wr;
        public MyHandler(MainActivity ma){
            wr = new WeakReference<MainActivity>(ma);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1) {
                wr.get().img.setVisibility(View.GONE);
                wr.get().txt.setText("打到了" + wr.get().numberClick + "只,共10只");

            }else if(msg.what == 2) {
                wr.get().img.setVisibility(View.VISIBLE);
            }else if(msg.what == 3) {
                wr.get().flag = false;
                wr.get().btn.setText("开始");
                wr.get().txt.setText("点击开始吧....");
                wr.get().img.setVisibility(View.GONE);
                wr.get().numberClick = 0;
            }
        }
    }

3.runOnUiThread rewrites the run method and can be used in the sub-thread to update the UI interface in the main thread

Guess you like

Origin blog.csdn.net/z1455841095/article/details/106254753