当前位置:网站首页>Using handler in a new thread

Using handler in a new thread

2022-07-05 06:27:00 v_ three billion four hundred and eighty-three million six hund

  1. The main thread is automatically created Handler, So there's no need to initialize
    . Be yourself new Thread When , Change, too ui Or prevent ANR Created on Handler
    In the thread, you need 1,Looper.prepare() 2,newHandler rewrite handlerMessage 3,looper.loop

The following procedure is to transmit a number ,Toas Show the prime numbers inside .

package com.example.handl;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    
    public final static String UPPER_NUM="upper";
    private EditText etNum;
    private  CalThread calThread;
    class  CalThread extends Thread{
    
        private Handler mHandler;

        @Override
        public void run() {
    
            super.run();
            Looper.prepare();
            mHandler=new Handler(){
    
                @Override
                public void handleMessage(@NonNull Message msg) {
    
                    super.handleMessage(msg);
                    if (msg.what==0x123){
    
                        int upper = msg.getData().getInt(UPPER_NUM);
                        List<Integer> nums = new ArrayList<>();
                        outer:
                        for (int i = 2; i < upper; i++) {
    
                            int j=2;
                            while(j<Math.sqrt(i)){
    
                                if(i!=2&&i%j==0){
    
                                    continue outer;
                                }
                                j++;
                            }
                            nums.add(i);
                        }
                        Toast.makeText(MainActivity.this,nums.toString(),Toast.LENGTH_LONG).show();
                    }
                }

            };
            Looper.loop();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etNum=findViewById(R.id.editText);
         calThread= new CalThread();
        calThread.start();
        Button bn    =findViewById(R.id.button);
        bn.setOnClickListener(view -> {
    
            Message msg=new Message();
            msg.what=0x123;
            Bundle bundle=new Bundle();
            bundle.putInt(UPPER_NUM,Integer.parseInt(etNum.getText().toString()));
            msg.setData(bundle);
            calThread.mHandler.sendMessage(msg);


        });
    }
}
原网站

版权声明
本文为[v_ three billion four hundred and eighty-three million six hund]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050620248763.html