Android学习记录—利用Handler在线程中传递数据
来源:互联网 发布:三星平板下载软件 编辑:程序博客网 时间:2024/06/03 15:19
时间紧迫,加上我也有一点疑惑,所以只放个代码。
MainActivity.class
public class MainActivity extends Activity implements View.OnClickListener{ private EditText editText; private Button button; private TextView textView; private ThreadTest threadTest; public Handler uihandler=new Handler(){ @Override public void handleMessage(Message msg) { textView.setText((msg.getData().getIntegerArrayList("list")).toString()); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.thread); init(); initlisten(); } private void init(){ editText=(EditText) findViewById(R.id.e_1); button=(Button) findViewById(R.id.b_1); textView=(TextView)findViewById(R.id.t_1); threadTest=new ThreadTest(uihandler); threadTest.start(); } private void initlisten(){ button.setOnClickListener(this); } @Override public void onClick(View v) { Message msg=new Message(); msg.what=1; Bundle bundle=new Bundle(); bundle.putInt("test",Integer.parseInt(editText.getText().toString())); msg.setData(bundle); threadTest.handler.sendMessage(msg); }}
ThreadTest.class
public class ThreadTest extends Thread { public android.os.Handler handler; public Handler uihandler; public ThreadTest(Handler mhandler){ uihandler=mhandler; } public void run(){ Looper.prepare(); handler=new Handler() { @Override public void handleMessage(Message msg) { ArrayList<Integer> nums=new ArrayList<Integer>(); if(msg.what==1){ int test=msg.getData().getInt("test"); outer: for (int i=2;i<=test;i++){ for (int j=2;j<=Math.sqrt(i);j++){ if(i!=2&&i%j==0){ continue outer; } } nums.add(i); } } Message message=new Message(); Bundle bundle=new Bundle(); bundle.putIntegerArrayList("list",nums); message.setData(bundle); uihandler.sendMessage(message); } }; Looper.loop(); }}
0 0
- Android学习记录—利用Handler在线程中传递数据
- Android 利用handler在线程之间传递代码
- Android 利用handler传递数据
- Android利用ThreadLocal进行Handler数据传递
- android利用回调函数在对话框中传递数据
- android利用回调函数在对话框中传递数据
- Handler利用bundle传递数据
- Android中利用Handler在子线程中更新界面--简单的小球上下跳动案例
- Android中利用Handler在子线程中更新界面--简单的小球上下跳动案例
- android通过Handler在线程之间传递消息
- android通过Handler在线程之间传递消息
- 【Android】在线程中使用Handler
- 利用Intent在Activity中传递数据
- Android Handler机制详解:在线程中新建Handler
- Android Handler/Looper/MessageQueue 在新线程中使用Handler
- android线程消息传递机制——Looper,Handler,Message
- android线程消息传递机制——Looper,Handler,Message
- Handler ,android消息传递,android 线程通信
- Git安装配置过程-Win7
- 腾讯云安装php服务器环境 LNMP
- Android debuggerd 源码分析
- 三十岁程序员的迷茫
- VIM简单配置
- Android学习记录—利用Handler在线程中传递数据
- python定时器
- 僵尸进程是如何产生的?怎样避免僵尸进程的产生
- java琐碎知识点1
- Guava学习笔记之排序
- 解决解决远程连接mysql错误1130
- 【Centos7 GRUB】修改开机等待时间
- MFC中Tab控件的使用详解
- 1020. 月饼 (25)