大佬教程收集整理的这篇文章主要介绍了总结Android中多线程更新应用的页面信息的方式,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_944_1@一、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();
public final void runOnUiThread(Runnable action){ if(Thread.currentThread()!=mUiThread){ mHandler.post(action); }else{ action.run(); } }
@H_944_1@二、新线程中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)"); } });
@H_944_1@三、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在主线程中。
post方式添加一个实现Runnable接口的匿名对象到消息对列中,在目标收到消息后就可以以回调的方式在自己的线程中执行
属性 | 类型 | 描述 |
---|---|---|
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定义的三种泛型
示例代码:
private class DownloadFilesTask extends AsyncTask<URL,Integer,Long> { protected Long doInBACkground(URl... urls) { int count = urls.length; long @R_883_10586@lSize = 0; for (int i = 0; i < count; i++) { @R_883_10586@lSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return @R_883_10586@lSize; } 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,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中多线程更新应用的页面信息的方式全部内容,希望文章能够帮你解决总结Android中多线程更新应用的页面信息的方式所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。