基本信息
源码名称:android os线程通讯实例 附完整源码
源码大小:0.06M
文件格式:.zip
开发语言:Java
更新时间:2013-04-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
mainactivity:
package com.test; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { android.os.Handler handler = new android.os.Handler() { public void handleMessage(Message message) { // System.out.println("handler"); // System.out.println(message.what); // System.out.println(message.arg1); // System.out.println(message.arg2); // System.out.println(message.obj); // System.out.println(message.getData().get("name")); text1.setText("hello"); } }; class MyTask extends AsyncTask { protected Object doInBackground(Object... objs) { System.out.println("doInBackground"); System.out.println(objs[0]); for (int i = 0; i < 10; i ) { try { Thread.sleep(1000); } catch (Exception ex) { ex.printStackTrace(); } this.publishProgress(i); } return "ok"; } protected void onProgressUpdate(Object... objs) { System.out.println("onProgressUpdate"); text1.setText(objs[0].toString()); } protected void onPostExecute(Object result) { System.out.println("onPostExecute"); text1.setText(result.toString()); } } android.widget.Button button1; android.widget.Button button2; android.widget.Button button3; android.widget.TextView text1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (android.widget.Button) this.findViewById(R.id.button1); button1.setOnClickListener(this); button2 = (android.widget.Button) this.findViewById(R.id.button2); button2.setOnClickListener(this); button3 = (android.widget.Button) this.findViewById(R.id.button3); button3.setOnClickListener(this); text1 = (android.widget.TextView) this.findViewById(R.id.text1); } public void onClick(View arg0) { switch (arg0.getId()) { case R.id.button1: // System.out.println("button1"); new Thread() { public void run() { try { // Thread.sleep(2000); Message message = handler.obtainMessage(); System.out.println(message.hashCode()); message.what = 1; message.arg1 = 2; message.arg2 = 3; message.obj = "anbo"; android.os.Bundle bundle = new android.os.Bundle(); bundle.putString("name", "handson"); message.setData(bundle); handler.sendMessage(message); // handler.sendEmptyMessageDelayed(2, 5000); // handler.removeMessages(2); } catch (Exception ex) { ex.printStackTrace(); } // System.out.println("button1 after"); } }.start(); break; case R.id.button2: System.out.println("button2"); break; case R.id.button3: /* * Thread tt = new Thread() { public void run() { * System.out.println("asdfasdf"); text1.setText("xxxxx"); try { * Thread.sleep(9000); } catch (Exception ex) { * ex.printStackTrace(); } } }; handler.post(tt); // * MainActivity.this.runOnUiThread(tt); // text1.post(tt); */ MyTask task = new MyTask(); task.execute("aa"); System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); break; } } }