首页 > 代码库 > handler的基础应用

handler的基础应用

包含了handler的三种简单应用 
1
public class MainActivity extends Activity implements OnClickListener{ 2 3 private static String TAG = "tag"; 4 private static ImageView img; 5 private Handler handler = new Handler(); 6 private static class handler1 extends Handler{ 7 // handler调用WeakReference来防止内存泄漏 17 */ 18 private WeakReference<MainActivity> mactivity; 19 public handler1(MainActivity activity){ 20 Log.v(TAG, "handler1-->handler1"); 21 mactivity = new WeakReference<MainActivity>(activity); 22 } 23 @Override 24 public void handleMessage(Message msg) { 25 // TODO Auto-generated method stub 26 MainActivity activity = mactivity.get(); 27 Log.v(TAG, "activity"+activity); 28 if (activity!=null) { 29 Log.v(TAG, "handler1-->handleMessage()"); 30 imgrandom(); 31 Log.v(TAG, "imgrandom()--msg.arg1:"+msg.arg1); 32 img.setBackgroundResource(images[msg.arg1]); 33 } 34 } 35 } 36 private handler1 hand = new handler1(MainActivity.this); 37 private MyRunable myranble = new MyRunable(); 38 // 图片的位置数组 39 private static int[] images = {R.drawable.t_a,R.drawable.t_asd,R.drawable.t_be,R.drawable.t_beaks, 40 R.drawable.t_beaksk,R.drawable.t_beasfke,R.drawable.t_beask,R.drawable.t_bebas,R.drawable.t_bebsfkkw, 41 R.drawable.t_beias,R.drawable.t_beisdfw,R.drawable.t_beisk,R.drawable.t_meksbbbgt,R.drawable.t_menbs, 42 R.drawable.t_mesdf,R.drawable.t_nebs,R.drawable.t_sd,R.drawable.t_weba,R.drawable.t_wiimmed,R.drawable.t_xifn}; 43 private static int index,baths = 1; 47 @Override 48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.activity_main); 51 img = (ImageView) findViewById(R.id.img1); 52 Log.v(TAG, "onCreate"); 54 } 55 // 按钮的监听事件 56 @Override 57 public void onClick(View v) { 58 // TODO Auto-generated method stub 59 switch (v.getId()) { 60 case R.id.btn1: 61 Log.v(TAG, "btn1"); 62 changimg(); 63 break; 64 case R.id.btn2: 65 Log.v(TAG, "btn2"); 66 if (baths == 1) { 67 loopimg(); 68 baths++; 69 Log.v(TAG, "baths:"+baths); 70 } 71 break; 72 case R.id.btn3: 73 Log.v(TAG, "btn3"); 74 threadchangimg(); 75 break; 76 case R.id.btn4: 77 Log.v(TAG, "btn4--baths:"+baths); 78 handler.removeCallbacks(myranble); 79 baths = 1; 80 break; 81 } 82 } 83 private void threadchangimg() { 84 // TODO Auto-generated method stub 85 // 创建一个子线程 86 new Thread(){ 87 @Override 88 public void run() { 89 // TODO Auto-generated method stub 90 Log.v(TAG, "threadchangimg()-->Thread()-->run()"); 91 /* 92 * 创建一个消息对象 把需要发送的消息放进对象中 通过sendMessage把此对象放 93 * 入系统的消息池中等待调用 94 */ 95 Message msg = new Message(); 96 imgrandom(); 97 msg.arg1 = index; 98 Log.v(TAG, "msg.arg1:"+msg.arg1); 99 hand.sendMessage(msg); 100 } 101 }.start(); 102 } 103 private void loopimg() { 104 // TODO Auto-generated method stub 105 Log.v(TAG, "loopimg()"); 106 // 延迟一秒通过主线程绑定的handler调用myranble 107 // 要先实例化MyRunable对象 108 handler.postDelayed(myranble, 1000); 109 } 110 class MyRunable implements Runnable{ 111 @Override 112 public void run() { 113 // TODO Auto-generated method stub 114 Log.v(TAG, "MyRunable-->run()"); 115 index++; 116 index = index%20; 117 Log.v(TAG, "index:"+index); 118 img.setBackgroundResource(images[index]); 119 handler.postDelayed(myranble, 1000); 120 } 121 122 } 123 private void changimg() { 124 // TODO Auto-generated method stub 125 /* 126 * 在子线程中不可以直接更新UI 127 * 需要通过主线程绑定的handler来更新UI 128 */ 129 Log.v(TAG, "changimg()"); 130 new Thread(){ 131 public void run() { 132 Log.v(TAG, "Thread()-->run()"); 133 handler.post(new Runnable() { 134 @Override 135 public void run() { 136 // TODO Auto-generated method stub 137 Log.v(TAG, "handler-->Runnable()-->run()"); 138 imgrandom(); 139 Log.v(TAG, "index:"+index); 140 img.setBackgroundResource(images[index]); 141 } 142 143 }); 144 }; 145 }.start(); 146 } 147 private static void imgrandom() { 148 // TODO Auto-generated method stub 149 // 实现Random方法来实现随机数 实例化一个Random对象 Random对象是int类型的 150 Log.v(TAG, "imgrandom()"); 151 Random random = new Random(); 152 index = random.nextInt(20); 153 } 154 }
对应的布局文件
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <Button
 9         android:id="@+id/btn1"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:onClick="onClick"
13         android:text="在子线程中更新UI" />
14 
15     <Button
16         android:id="@+id/btn2"
17         android:layout_width="match_parent"
18         android:layout_height="wrap_content"
19         android:onClick="onClick"
20         android:text="安卓中的图片轮播" />
21 
22     <Button
23         android:id="@+id/btn3"
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         android:onClick="onClick"
27         android:text="通过消息队列实现图片更改" />
28 
29     <Button
30         android:id="@+id/btn4"
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:onClick="onClick"
34         android:text="停止正在运行的线程" />
35 <RelativeLayout 
36         android:layout_width="match_parent"
37         android:layout_height="match_parent">
38       <ImageView
39         android:id="@+id/img1"
40         android:layout_centerHorizontal="true"
41         android:layout_centerVertical="true"
42         android:layout_width="wrap_content"
43         android:layout_height="wrap_content"
44         android:background="@drawable/t_beaksk"/>
45 </RelativeLayout>
46 
47 </LinearLayout>

 

 

handler的基础应用