android线程通信之Asynctask
2016-04-22 09:40
465 查看
这篇文章我们来谈谈android线程通信中的Asynctask,首先这是一个抽象的类,如果我们要使用它,我们必须有一个它的实现子类,并重写它的四个方法,在我的代码之中,我对每个方法都有详细的介绍,因为一个Asynctask只能execute一次,所以我的这个计数程序有点小问题,你们可以通过暂停等按钮的不可点击,然后再启动按钮以后再恢复按钮的点击,当然启动按钮会重新给一个新的对象,我把代码贴出来大家参考。当然,和以往一样,布局代码就不贴了,只贴java代码
package com.jk.asynctaskdemo; import android.os.AsyncTask; import android.os.Bundle; 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 implements OnClickListener { //declare variable object TextView tv; MyTask myTask; Button btn_start, btn_begin, btn_pause, btn_stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void init() { //bundle the widget with the id tv = (TextView) findViewById(R.id.tv_count); btn_start = (Button) findViewById(R.id.btn_start); btn_begin = (Button) findViewById(R.id.btn_begin); btn_pause = (Button) findViewById(R.id.btn_pause); btn_stop = (Button) findViewById(R.id.btn_stop); //set the onclick event btn_start.setOnClickListener(this); btn_begin.setOnClickListener(this); btn_pause.setOnClickListener(this); btn_stop.setOnClickListener(this); //instantiation myTask myTask = new MyTask(); } @Override public void onClick(View v) { //judge different click event switch (v.getId()) { case R.id.btn_start: myTask.isrun = true; myTask.execute(0); break; case R.id.btn_begin: myTask.ispause = false; break; case R.id.btn_pause: myTask.ispause = true; break; case R.id.btn_stop: myTask.isrun = false; } } //first params,the type of the parameter that you need to send when you launch the task //second progress,the type of the parameter that be send when the onProgressUpdata method is running //third result,the type of doInBackground method return,and the onPostExecute receive class MyTask extends AsyncTask<Integer, Integer, Integer> { int i = 0; boolean isrun; boolean ispause; //the method is running in the subthread,always run the longtime task @Override protected Integer doInBackground(Integer... params) { while (isrun) { while(ispause){ } //call the onProgressUpdata method publishProgress(++i); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } //running in main thread ,and running after doInBackground @Override protected void onPostExecute(Integer result) { // TODO Auto-generated method stub super.onPostExecute(result); } //running in main thread,and running before doInBackground @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); } //running in main thread,and called by publishProgress @Override protected void onProgressUpdate(Integer... values) { tv.setText(""+values[0]); super.onProgressUpdate(values); } } }
相关文章推荐
- android 布局属性大全
- android paint 研究总结
- 如何自学Android
- AndroidSDK更新慢和Android Studio老连接不到外网的解决方式分享一下
- Android之简单页面跳转
- Android中string.xml使用总结
- Android 中布局设置导致的TextView不显示的问题
- Android Fragment 基本介绍
- view事件传递
- Android开发笔记(九十)建造者模式
- Android Log统一管理类
- 获取android keystore的SHA1值
- Android Scroll 详解
- 关于Android创建Activity需要注意的地方
- #Android#常用控件使用方法
- 64位Ubuntu配置sdk
- Android_实用技术(3)—— Service简析(I)
- Android中 一些维持状态的经验
- 基于android的Socket通信
- Android:Activity的跳转