从另一个类线程更新的Andr​​oid UI [英] Update Android UI from a thread in another class

查看:196
本文介绍了从另一个类线程更新的Andr​​oid UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了在这里问类似的问题了几个问题,但我还没有看到合适的答案。很多人都问如何更新从一个线程的用户界面,但他们几乎总是在同一类的UI。

我试图做的是更新,从已在另一个类中创建一个线程的用户界面。我见过的所有的建议,比如异步,处理程序,可运行,等等...但我有真正的麻烦实施这些单独的类。

我试图让我的UI类很少,只处理与图形用户界面交互,当用户presses按钮等。现在,我创建了一个新的线程,在一个新的类,它连接到蓝牙设备,但后来我想从一个连接按钮,将断开按钮更改UI线程按钮(即变化从创建蓝牙套接字关闭它)按钮。

什么是做到这一点的一般方法是什么?我在想这一切错误的,应该一切都在一个班?什么是正确的方式主的UI类和其他类之间进行交互/线程?

理想我希望能够做其他的UI交互,所以一些解决方案,它可以使UI类之外的其他用户界面的变化将是巨大的!

解决方案
  

我试图要做的就是更新从一个线程的用户界面已经被   在另一个类中创建的。我见过的所有的建议,如   异步,处理程序,可运行,等等...但我有真正的麻烦   实施这些单独的类。

通常对于自己的目标,我建议你使用:

  • <一个href="http://developer.android.com/reference/android/os/AsyncTask.html"><$c$c>AsyncTask
  • <一个href="http://developer.android.com/reference/android/app/IntentService.html"><$c$c>IntentService与<一个href="http://developer.android.com/reference/android/os/ResultReceiver.html"><$c$c>ResultReceiver

我不认为它太棘手。绝对不。如果你拥有了它作为单独的类(ES)而不是内部类(ES)在某些活动类,所以我建议使用构造函数,你会通过上下文,窗口小部件,一般你想,然后在正确的方法(它允许用户界面更新)更新无论你的 UI

我这么做,是因为我喜欢当我有清洁类(所以UI类只有UI实现和逻辑单独放置)。

例:

 公共类TaskExample扩展的AsyncTask&LT;太虚,整型,太虚&GT; {

   私人语境℃;
   私人键b;

   公共TaskExample(上下文C,键b){
      this.c = C;
      this.b = B;
   }

   保护无效doInBackground(虚空...... PARAMS){
      // 一些工作
      如果(isSomethingConnected){
         publishProgress(Constants.IS_CONNECTED);
      }
      返回null;
   }

   公共无效onProgressUpdate(整数... PARAMS){
       开关(PARAMS [0]){
          案例Constants.IS_CONNECTED:
             b.setText(已连接);
          打破;
          案例Constants.ANOTHER_CONSTANT:
             //另一项工作
          打破;
       }
   }
}
 

用法:

 公共类主要活动扩展实现View.OnClickListener {

   私人键b;

   公共无效的onCreate(包B){
      super.onCreate(B);
      //初始化小部件,并设置监听到合适的部件
   }

   公共无效的onClick(视图v){
      开关(v.getId()){
         案例R.id.connectBtn:
            startWorker();
         打破;
      }
   }

   私人无效startWorker(){
      TaskExample TE =新TaskExample(这一点,B);
      te.execute();
   }
}
 

I've seen a few questions on here asking similar questions, but I've not yet seen a suitable answer. Many people have asked how to update the UI from a thread, but they're almost always in the same class as the UI.

What I'm trying to do is update the UI from a thread which has been created in another class. I've seen all of the suggestions, such as async, handlers, runnable, etc... but I've having real trouble implementing them in separate classes.

I'm trying to keep my UI class minimal and only deal with interactions with the GUI, such as when a user presses a button. Now, I've created a new thread, in a new class, which connects to a Bluetooth device, but I then want to change a button in the UI thread from being a 'connect' button to a 'disconnect' button (i.e. change the button from creating the Bluetooth socket to closing it).

What is the general way to do this? Am I thinking of this all wrong and should have everything in one class? What is the correct way to interact between the 'main' UI class and other classes/threads?

Ideally I want to be able to do other UI interactions, so some solution which allows other UI changes outside of the UI class would be great!

解决方案

What I'm trying to do is update the UI from a thread which has been created in another class. I've seen all of the suggestions, such as async, handlers, runnable, etc... but I've having real trouble implementing them in separate classes.

Generally for your goal i recommend to you use:

I don't think that its too tricky. Absolutely not. If you have it as separated class(es) and not as inner class(es) in some Activity class so i recommend to use constructor where you will pass context, widgets, generally whatever you want and then in correct methods(which allows UI update) update your UI.

I'm doing it because i like when i have clean classes(so UI class have only UI implementations and logic is positioned separately).

Example:

public class TaskExample extends AsyncTask<Void, Integer, Void> {

   private Context c;
   private Button b;

   public TaskExample(Context c, Button b) {
      this.c = c;
      this.b = b;
   }

   protected Void doInBackground(Void... params) {
      // some work
      if (isSomethingConnected) {
         publishProgress(Constants.IS_CONNECTED);
      }
      return null;
   }

   public void onProgressUpdate(Integer... params) {
       switch (params[0]) {
          case Constants.IS_CONNECTED:
             b.setText("Connected");
          break;
          case Constants.ANOTHER_CONSTANT:
             // another work
          break;
       }
   }  
}

Usage:

public class Main extends Activity implements View.OnClickListener {

   private Button b;

   public void onCreate(Bundle b) {
      super.onCreate(b);
      // initialise widgets and set listeners to appropriate widgets
   }

   public void onClick(View v) {
      switch(v.getId()) {
         case R.id.connectBtn:
            startWorker();
         break;
      }
   }

   private void startWorker() {
      TaskExample te = new TaskExample(this, b);
      te.execute();
   }
}

这篇关于从另一个类线程更新的Andr​​oid UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆