如何使用runOnUiThread

我正在学习在UI-Thread中做些什么,所以我写了简单的testing活动。 但是我想我误解了一些东西,因为当我点击button时 – 应用程序不再支持

public class TestActivity extends Activity { Button btn; int i = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { runThread(); } }); } private void runThread(){ runOnUiThread (new Thread(new Runnable() { public void run() { while(i++ < 1000){ btn.setText("#"+i); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } })); } } 

下面是runThread函数的更正片段。

 private void runThread() { new Thread() { public void run() { while (i++ < 1000) { try { runOnUiThread(new Runnable() { @Override public void run() { btn.setText("#" + i); } }); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } 

把它作为一个函数包装起来,然后从后台线程中调用这个函数。

 public void debugMsg(String msg) { final String str = msg; runOnUiThread(new Runnable() { @Override public void run() { mInfo.setText(str); } }); } 

你有它回到前面。 您的button点击会导致对runOnUiThread()的调用,但这不是必需的,因为点击处理程序已经在UI线程上运行。 然后, runOnUiThread()代码将启动一个新的后台线程,尝试执行UI操作,然后失败。

相反,直接从您的点击处理程序启动后台线程。 然后,在对btn.setText()的调用中将调用包装到runOnUiThread()

 runOnUiThread(new Runnable() { public void run() { //Do something on UiThread } }); 

有几种使用runOnUiThread()的技术,让我们看看全部

这是我的主线程(UI线程)称为AndroidBasicThreadActivity ,我打算以各种方式从工作线程更新它 –

 public class AndroidBasicThreadActivity extends AppCompatActivity { public static TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_basic_thread); textView = (TextView) findViewById(R.id.textview); MyAndroidThread myTask = new MyAndroidThread(AndroidBasicThreadActivity.this); Thread t1 = new Thread(myTask, "Bajrang"); t1.start(); } } 

1.)通过将Activity的实例作为parameter passing给工作线程

 class MyAndroidThread implements Runnable { Activity activity; public MyAndroidThread(Activity activity) { this.activity = activity; } @Override public void run() { activity.runOnUiThread(new Runnable() { @Override public void run() { AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread."); } }); } } 

2.)通过在工作线程中使用View的post(Runnable runnable)方法

 class MyAndroidThread implements Runnable { Activity activity; public MyAndroidThread(Activity activity) { this.activity = activity; } @Override public void run() { AndroidBasicThreadActivity.textView.post(new Runnable() { @Override public void run() { AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread."); } }); } } 

3.)通过使用android.os包中的Handler类如果我们没有上下文(this / getApplicationContext())或Activity的实例(AndroidBasicThreadActivity.this),那么我们必须使用如下的Handler类 –

 class MyAndroidThread implements Runnable { Activity activity; public MyAndroidThread(Activity activity) { this.activity = activity; } @Override public void run() { new Handler(Looper.getMainLooper()).post(new Runnable() { public void run() { AndroidBasicThreadActivity.textView.setText("Hello!! Android Team :-) From child thread."); } }); } } 

你可以使用这个示例:

在以下示例中,我们将使用此工具来发布由后台线程处理的同义词search的结果。

为了在OnCreate活动callback期间完成目标,我们将设置onClickListener在创build的线程上运行searchTask。

当用户点击searchbutton时,我们将创build一个Runnable匿名类,用于searchR.id.wordEt EditText中键入的单词并启动线程以执行Runnable。

search完成后,我们将创build一个Runnable SetSynonymResult的实例,将结果发布到UI线程上的同义TextView上。

这个技术有时不是最方便的,特别是当我们没有访问一个Activity实例时; 因此,在下面的章节中,我们将讨论更简单,更清洁的技术来从后台计算任务更新UI。

 public class MainActivity extends AppCompatActivity { class SetSynonymResult implements Runnable { String synonym; SetSynonymResult(String synonym) { this.synonym = synonym; } public void run() { Log.d("AsyncAndroid", String.format("Sending synonym result %s on %d", synonym, Thread.currentThread().getId()) + " !"); TextView tv = (TextView) findViewById(R.id.synonymTv); tv.setText(this.synonym); } } ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button search = (Button) findViewById(R.id.searchBut); final EditText word = (EditText) findViewById(R.id.wordEt); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Runnable searchTask = new Runnable() { @Override public void run() { String result = searchSynomim(word.getText().toString()); Log.d("AsyncAndroid", String.format("Searching for synonym for %s on %s", word.getText(), Thread.currentThread().getName())); runOnUiThread(new SetSynonymResult(result)); } }; Thread thread = new Thread(searchTask); thread.start(); } }); } static int i = 0; String searchSynomim(String word) { return ++i % 2 == 0 ? "fake" : "mock"; } } 

来源

asynchronousandroid编程Helder Vasconcelos

这是我如何使用它:

 runOnUiThread(new Runnable() { @Override public void run() { //Do something on UiThread } }); 

你这个:

 @UiThread public void logMsg(final String msg) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { Log.d("UI thread", "I am the UI thread"); } }); } 
  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gifImageView = (GifImageView) findViewById(R.id.GifImageView); gifImageView.setGifImageResource(R.drawable.success1); new Thread(new Runnable() { @Override public void run() { try { //dummy delay for 2 second Thread.sleep(8000); } catch (InterruptedException e) { e.printStackTrace(); } //update ui on UI thread runOnUiThread(new Runnable() { @Override public void run() { gifImageView.setGifImageResource(R.drawable.success); } }); } }).start(); }