亲宝软件园·资讯

展开

安卓多线程更新应用页面 总结Android中多线程更新应用的页面信息的方式

时光微凉 人气:0
想了解总结Android中多线程更新应用的页面信息的方式的相关内容吗,时光微凉在本文为您仔细讲解安卓多线程更新应用页面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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接口的匿名对象到消息对列中,在目标收到消息后就可以以回调的方式在自己的线程中执行

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定义的三种泛型

 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();
    }    
   }  
  }

加载全部内容

相关教程
猜你喜欢
用户评论