Android Handler/Looper/MessageQueue 在新线程中使用Handler

来源:互联网 发布:列数据的句子 编辑:程序博客网 时间:2024/05/17 03:13


在线程中使用Handler步骤:

1.调用Handler的prepare()方法为当前线程创建Looper对象,创建Looper对象时,它的构造器会创建与之匹配的MessageQueue.

2.创建Handler子类的实例,并重写handleMessage方法

3.调用Looper的loop()方法启动Looper.


class newThread extends Thread

{

Handler mHandler

public void run()

{

Looper.prepare();

mHandler = new Handler()

{

public void handleMessage(Message msg)

{

if(msg.what == 0x123)

........

}

};

Loopper.loop();

}

}




<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="shortcut.song.com.myapplication.LopperActivity">    <EditText        android:id="@+id/input_et"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="cal"        /></LinearLayout>


package shortcut.song.com.myapplication;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class LopperActivity extends AppCompatActivity {    static final String UPPER_NUM = "upper";    EditText mEditText;    CalThread mCalThread;    class CalThread extends Thread {        public Handler mHandler;        @Override        public void run() {            super.run();            Looper.prepare();            mHandler = new Handler(){                @Override                public void handleMessage(Message msg) {                    super.handleMessage(msg);                    if (msg.what == 0x123)                    {                        int upper = msg.getData().getInt(UPPER_NUM);                        List<Integer> nums = new ArrayList<Integer>();                        outer:                        for (int i=2; i<=upper; i++)                        {                            for(int j=2; j <= Math.sqrt(i); j++)                            {                                if (i != 2 && i % j == 0)                                {                                    continue outer;                                }                            }                            nums.add(i);                        }                        Toast.makeText(LopperActivity.this, nums.toString(), Toast.LENGTH_SHORT).show();                    }                }            };            Looper.loop();        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_lopper);        mEditText = (EditText)findViewById(R.id.input_et);        mCalThread = new CalThread();        mCalThread.start();    }    public void cal(View v)    {        Message msg = new Message();        msg.what = 0x123;        Bundle mBundle = new Bundle();        mBundle.putInt(UPPER_NUM, Integer.parseInt(mEditText.getText().toString()));        msg.setData(mBundle);        mCalThread.mHandler.sendMessage(msg);    }}

0 0
原创粉丝点击