总结Android中多线程更新应用的页面信息的方式
时间:2021-08-04 08:32:46|栏目:Android代码|点击: 次
一、runOnUiThread的用法
runOnUiThread是Activity的内部方法,使用时最好指定当前的环境变量(Context)。
new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { public void run() { Toast.makeText(mainActivity.this,"UI操作。。。",0).show(); } }); } }).start();
执行runOnUiThread这个方法会调用父类中的
public final void runOnUiThread(Runnable action){ if(Thread.currentThread()!=mUiThread){ mHandler.post(action); }else{ action.run(); } }
二、新线程中View直接在UI线程中更新的方法
textView.postDelayed(new Runnable() { @Override public void run() { textView.setText("Test View.post(Runnable)"); } }, 1000); - textView.post(new Runnable() { @Override public void run() { textView.setText("Test View.postDelay(Runnable,long)"); } });
三、Handler(消息传递机制)使用
Handler myHandler = new Handler(){ public void handleMessage(Message msg){ super.handleMessage(msg); } };
也可以继承handler
class MyHandler extends handler{ public MyHandler(){ } @Override public void handleMessage(Message msg){ super.handleMessage(msg); } }
分发Message或者Runnable对象到handler所在的线程中一般handler在主线程中。
handler中一些分发消息的方法:
- post(Runnable)
- postAtTime(Runnable,long)
- postDelay(Runnable,long)
- sendEmptyMessage(int what)
- sendMessage(Message)
- senMessageAtTime(Message,long)
- sendMessageDelayed(Message,long)
post方式添加一个实现Runnable接口的匿名对象到消息对列中,在目标收到消息后就可以以回调的方式在自己的线程中执行
Message对象所具有的属性:
属性 | 类型 | 描述 |
---|---|---|
arg1 | int | 用来存放整型数据 |
arg2 | int | 用来存放整型数据 |
obj | Object | 用来存放发送给接收器的Object任意对象 |
replyTo | Messager | 用来指定此Message发送到何处的可选Message对象 |
what | int | 用于指定用户自定义的消息代码这样接受者就可以了解这个消息的信息 |
Message message = Message.obtain(); message.arg1 = 1; message.arg2 = 2; message.obj = "Demo"; message.what = 3; Bundle bundle = new Bundle(); bundle.putString( "name","Lucy"); message.setData(bundle);
下面贴上一段示例代码(开启新线程实现电子广告牌)
public class MainActivity extends Activity implements Runnable { private ImageView iv; private TextView tv; private Handler handler; private int[] path = new int[]{R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04, R.drawable.img05,R.drawable.img06}; private String[] title = new String[]{"编程词典系列","高效开发","快乐分享","用户人群","快速学习","全方位查询"}; private int index =0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); tv = (TextView) findViewById(R.id.textView1); Thread t = new Thread(this); t.start(); handler = new Handler(){ public void handleMessage(Message msg){ if (msg.what ==1) { tv.setText(msg.getData().getString("title")); iv.setImageResource(path[msg.arg1]); } super.handleMessage(msg); } }; } @Override public void run() { while(!Thread.currentThread().isInterrupted()){ index = new Random().nextInt(path.length); Message m = handler.obtainMessage(); m.arg1 = index; Bundle bundle = new Bundle(); m.what = 1; bundle.putString("title", title[index]); m.setData(bundle); handler.sendMessage(m); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
四、AsyncTask异步任务的用法
- AsyncTask实际上是一个线程池,在代码上比handler要轻量级但是实际上要比Handler要耗资源,Handler仅仅发送了一个消息队列,连线程池对没有开。
- onPreExecute(),(可选方法)最新用户调用excute时的接口,任务执行之前调用该方法,可以在这里显示进度对话框。
- doInBackground(Params...),后台执行比较好使的操作,不能直接操纵UI。在该方法中使用publishProgress(progress...)来更新任务的进度。
- onProgressUpdate(Progress...),在主线程中执行,显示进度条
- onPostExecute(Result),此方法可以从doinbackground得到的结果来操作UI,在主线程中执行,执行的结果作为参数返回。
- onCancelled(Object)调用此方法可以随时取消操作。
AsyncTask定义的三种泛型
- params: 启动任务执行的输入参数,如:http请求的URL
- progress:后台任务执行的百分比
- result:返回结果,如:String、list集合等
private class MyTask extends AsyncTask<params, progress, result> { ... }
示例代码:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }
获取网络图片的示例代码:
public class MainActivity extends ActionBarActivity { private ImageView iv; private Button bt; private String imagePath = "http://192.168.1.1/sa"; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.imageView1); bt = (Button) findViewById(R.id.button1); dialog = new ProgressDialog(this); dialog.setTitle("提示信息:"); dialog.setMessage("正在下载。。。"); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new MyTask().execute(imagePath); } }); } public class MyTask extends AsyncTask<String, Void, Bitmap>{ @Override protected Bitmap doInBackground(String... params) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); Bitmap bitmap = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==200) { HttpEntity httpEntity = httpResponse.getEntity(); byte[] data = EntityUtils.toByteArray(httpEntity); bitmap= BitmapFactory.decodeByteArray(data, 0, data.length); } } catch (Exception e) { e.printStackTrace(); } return bitmap; } @Override protected void onPreExecute() { super.onPreExecute(); dialog.show(); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); iv.setImageBitmap(result); dialog.dismiss(); } } }
上一篇:Android实战教程第四十篇之Chronometer实现倒计时
栏 目:Android代码
本文标题:总结Android中多线程更新应用的页面信息的方式
本文地址:http://www.codeinn.net/misctech/163977.html