使用新线程处理事件



Looper 没个线程都只有一个Looper 负责管理MessageQueue,不断从中读取Message,并交给Hanlder处理

MssageQueue Looper管理里,先进先出

Handler 消息发送给MessageQueue,并可以处理Looper分给的信息


1,调用Looper.prepare()  创建当前线程的Looper,MessageQueue会被Looper的构造器直接创建配套的。

2,创建Handler子类,重写handlerMessage() ,处理消息

3,Looper.loop()   启动Looper。


这是疯狂android上的例子  计算质数

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numeric="integer" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交" />

</LinearLayout>


package main;

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

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.calprime.R;

public class Main extends Activity implements OnClickListener
{
	public CalThread cal;
	private EditText et;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		cal = new CalThread();
		et = (EditText)findViewById(R.id.edittext);
		new Thread(cal).start();
		Button b = (Button)findViewById(R.id.button);
		b.setOnClickListener(this);
	}
	public void onClick(View source)
	{
		Message msg = new Message();
		msg.what = 123;
		Bundle b = new Bundle();
		b.putInt("NUM", Integer.parseInt(et.getText().toString()));
		msg.setData(b);
		cal.h.sendMessage(msg);
	}
	class CalThread implements Runnable
	{
		public Handler h;
		public CalThread()
		{
			// TODO Auto-generated constructor stub
		}
		@Override
		public void run()
		{
			// TODO Auto-generated method stub
			Looper.prepare();
			h = new Handler()
			{
				@Override
				public void handleMessage(Message msg)
				{
					// TODO Auto-generated method stub
					super.handleMessage(msg);
					if(msg.what == 123)
					{
						int upper = msg.getData().getInt("NUM");
						List<Integer> nums = new ArrayList<Integer>();
						for(int i = 2;i<=upper;i++)
						{
							if(isPrime(i))
							{
								nums.add(i);
							}
						}
						Toast.makeText(Main.this, nums.toString(), Toast.LENGTH_LONG).show();
						
					}
				}
			};
			Looper.loop();
		}
		public boolean isPrime(int num)
		{
			for(int i=2;i<Math.sqrt(num);i++)
			{
				if(num%i==0)
				{
					return false;
				}
			}
			return true;
		}
	}
	
	
}



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity android:name="main.Main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值