Android handler异步处理简单例子

时间:2022-08-27 15:40:45

public class HandlerActivity extends BaseActivity {    private static final String UPPER_NUM = "upper";    private static final String TV_NUM = "tvnum";    private EditText edit_prime;    private CalThread calThread;    private TextView tv_allPrime;    Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            if (msg.what==0x1234){                ArrayList<Integer> nums=msg.getData().getIntegerArrayList(TV_NUM);                if (nums!=null) {                    tv_allPrime.setText(nums.toString());                }            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_getprime);        initViews();    }    private void initViews() {        edit_prime = (EditText) findViewById(R.id.edit_prime);        tv_allPrime= (TextView) findViewById(R.id.tv_allPrime);        calThread=new CalThread();        calThread.start();    }    /**      为按钮点击事件提供事件处理函数     */    public void cal(View view) {        //创建消息        Message message=new Message();        message.what=0x123;        Bundle bundle=new Bundle();        bundle.putInt(UPPER_NUM,Integer.parseInt(edit_prime.getText().toString()));        message.setData(bundle);        calThread.mhandler.sendMessage(message);    }    class CalThread extends Thread {        public Handler mhandler;        @Override        public void run() {            //获取新的looper            Looper.prepare();            mhandler = new Handler() {                @Override                public void handleMessage(Message msg) {                    if (msg.what == 0x123) {                        int upper = msg.getData().getInt(UPPER_NUM);                        ArrayList<Integer> nums = new ArrayList<>();                        //从2开始计算到upper的所有质数                        outer:                        for (int i = 2; i < upper; i++) {                            //从2开始除,到i的平方根为止                            for (int j = 2; j <=Math.sqrt(i); j++) {                                //如果可以整除,则表明不是质数,跳出j这个循环                                if (i != 2 && i % j == 0) {                                    continue outer;                                }                            }                            nums.add(i);                        }                        ToastUtil.showLong(HandlerActivity.this,nums.toString());                        Message message=new Message();                        message.what=0x1234;                        Bundle bundle=new Bundle();                        bundle.putIntegerArrayList(TV_NUM, nums);                        message.setData(bundle);                        handler.sendMessage(message);                    }                }            };            //启动loop            Looper.loop();        }    }}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt_getPrime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="cal"
android:text="@string/activity_getPrime_btn_getPrime"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/edit_prime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/bt_getPrime"
android:layout_alignBottom="@+id/bt_getPrime"
/>
<!--显示所有质数-->
<TextView
android:id="@+id/tv_allPrime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_prime"
android:layout_centerInParent="true" />
</RelativeLayout>