Android handle 多线程练习
Android handle
1 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/text_id" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 <Button 17 android:id="@+id/btn" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="Button" 21 android:layout_below="@id/text_id" 22 23 /> 24 25 </RelativeLayout>
1 package com.ibox365.s0725001; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity { 13 14 private Button button; 15 private Handler handler; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 button=(Button) findViewById(R.id.btn); 23 button.setOnClickListener(new ButtonListen()); 24 25 handler=new myHandle(); 26 27 } 28 29 class ButtonListen implements OnClickListener 30 { 31 32 /* (non-Javadoc) 33 * @see android.view.View.OnClickListener#onClick(android.view.View) 34 */ 35 @Override 36 public void onClick(View v) { 37 // TODO Auto-generated method stub 38 39 Message msg=handler.obtainMessage(); 40 msg.what=2; 41 handler.sendMessage(msg); 42 } 43 44 45 } 46 47 class myHandle extends Handler{ 48 49 /* (non-Javadoc) 50 * @see android.os.Handler#handleMessage(android.os.Message) 51 */ 52 @Override 53 public void handleMessage(Message msg) { 54 // TODO Auto-generated method stub 55 super.handleMessage(msg); 56 57 int what =msg.what; 58 System.out.println(what); 59 60 } 61 62 63 } 64 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 // Inflate the menu; this adds items to the action bar if it is present. 68 getMenuInflater().inflate(R.menu.main, menu); 69 return true; 70 } 71 72 }
多线程 操作
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送消息" /> </LinearLayout>
package com.ibox365.s0725002; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button button; private TextView textView; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.btn_id); button.setOnClickListener(new ButtonListen()); textView=(TextView) findViewById(R.id.text_id); handler=new MyHandle(); } class ButtonListen implements OnClickListener { @Override public void onClick(View v) { Thread thread=new MyThread(); thread.start(); } } class MyThread extends Thread{ @Override public void run() { System.out.println("MyThread==>"+Thread.currentThread().getName()); try { Thread.sleep(2*1000); } catch (InterruptedException e) { e.printStackTrace(); } String s="get data from network!"; Message msg=handler.obtainMessage(); msg.obj=s; handler.sendMessage(msg); } } class MyHandle extends Handler{ @Override public void handleMessage(Message msg) { System.out.println("MyHandle==>"+Thread.currentThread().getName()); String s=(String) msg.obj; textView.setText(s); } } }
思路图:
转载 请注明原文地址并标明转载:http://www.cnblogs.com/laopo
商业用途请与我联系:lcfhn168@163.com