因最近老是写android 4.4以上的程序,handler是非学不可了。
直接上代码,第一段是简单的在主线程中使用Handler,完成一个定时自动换图片的功能。
布局就不写了,就一个ImageView
activity代码:
package com.example.handler; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private int[] imgArr = new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3} ; private int currentItem ; private ImageView img ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.currentItem = 0 ; this.img = (ImageView) findViewById(R.id.img) ; final Handler hand = new Handler() { @Override public void handleMessage(android.os.Message msg) { if(msg.what == 0x123) { img.setImageResource(imgArr[(currentItem++)%imgArr.length]); } }; } ; new Timer().schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub hand.sendEmptyMessage(0x123) ; } }, 0,800); } @Override protected void onPause() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "pause", 1000) ; System.out.println("pause"); super.onPause(); } }
第二个demo是在子线程中向主线程中发送信息。借助Looper.prepare自动生成MessageQueue来按照先进先出的原则处理接受到的message,一时想不到好的例子,就直接copy李刚老师的代码了,输入一个上限,然后用toast显示出所有的满足题条件的素数。
布局代码:
<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/etNum" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入上限"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="cal" android:text="计算"/> </LinearLayout>
主activity代码
package com.example.handler; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { /* * 重新创建一个onCreate() */ static final String UPPER_NUM = "upper" ; CalThread calthread ; //定义一个新线程 private EditText etNum ; class CalThread extends Thread { public Handler handler ; @Override public void run() { // TODO Auto-generated method stub Looper.prepare(); handler = new Handler() { public void handleMessage(android.os.Message msg) { if(msg.what == 0x123) { int upper = msg.getData().getInt(UPPER_NUM) ; List<Integer> list = new ArrayList<Integer>() ; /* * 开始计算素数 */ outer : for (int i = 2 ; i <= upper ; i++) { // 用i处于从2开始、到i的平方根的所有数 for (int j = 2 ; j <= Math.sqrt(i) ; j++) { // 如果可以整除,表明这个数不是质数 if(i != 2 && i % j == 0) { continue outer; } } list.add(i); } Toast.makeText(getApplicationContext(), list.toString(),Toast.LENGTH_LONG).show(); } } } ; Looper.loop(); } } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); this.etNum = (EditText) findViewById(R.id.etNum) ; calthread = new CalThread() ; calthread.start() ; } public void cal(View source) { Message msg = new Message() ; msg.what = 0x123 ; Bundle bundle = new Bundle() ; bundle.putInt(UPPER_NUM, Integer.parseInt(this.etNum.getText().toString())); msg.setData(bundle); calthread.handler.sendMessage(msg) ; } }