2013-05-10 58 views
0

我对android非常陌生,只是想知道它是否有任何工作和可能的方式来更新主线程之外的用户界面。从我的代码中我已经列出下面我知道从这个代码;这是不可能的。但是,事情是我只想使用另一个线程更新UI。请提前帮助我,谢谢!更新主线程外的用户界面

package com.example.app; 

import java.util.Random; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
import android.view.View; 
    import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
private Button b; 
public ImageView I1; 
public ImageView I2; 
public ImageView I3; 
public ImageView I4; 
public TextView T; 
public TextView s; 
@Override 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

I1=new ImageView(this); 
I1=(ImageView) findViewById(R.id.imag1); 
I1.setVisibility(View.INVISIBLE); 

I2=new ImageView(this); 
I2=(ImageView) findViewById(R.id.imag2); 
I2.setVisibility(View.INVISIBLE); 

I3=new ImageView(this); 
I3=(ImageView) findViewById(R.id.imag3); 
I3.setVisibility(View.INVISIBLE); 

I4=new ImageView(this); 
I4=(ImageView) findViewById(R.id.imag4); 
I4.setVisibility(View.INVISIBLE); 

T=(TextView)findViewById(R.id.time); 
s=(TextView)findViewById(R.id.score); 

Thread t=new Thread(new MyThread()); 
t.start(); 
    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 

private class MyThread implements Runnable{ 
Random randomGenerator = new Random(); 
int n; 
public void run(){ 

    while(true){ 
     n=randomGenerator.nextInt(8); 

     if(n==1){ 
      I1.setVisibility(View.VISIBLE); 

     } 
     if(n==2){ 
      I2.setVisibility(View.VISIBLE); 
     } 

     if(n==3){ 
      I3.setVisibility(View.VISIBLE); 
     } 

     if(n==4){ 
      I4.setVisibility(View.VISIBLE); 
     } 
     try { 
      Thread.currentThread().sleep(500); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.getStackTrace(); 
     } 
    } 

    } 




    } 
    } 
+1

的方法后,您应该了解更多关于Android和多线程,你的代码有设计缺陷。 用户界面不应该由主线程的任何其他线程来更新,如果您有麻烦,请阅读文档。然而,android提供的工具可以让你从其他线程发送的oders更新UI,但是你必须阅读文档来弄清楚。 – 2013-05-10 13:31:48

+1

你可以给我关于该特定文档的链接吗? – 2013-05-10 13:33:59

+1

在这里,首先:[进程和线程](http://developer.android.com/guide/components/processes-and-threads.html) – 2013-05-10 13:45:43

回答

0

只有主线程才能更新UI。如果您的线程想要更​​新UI,请使用activity.runOnUiThread()

但是,您的操作只是排队在UI线程中运行(如果您不是从UI线程运行它),这意味着它可以稍微更新UI后来。

http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

+0

但我可以使用睡眠方法来暂停执行。我的意思是,如果在RunOnUIThread()中使用睡眠,主线程将导致暂停,这会造成问题! – 2013-05-10 13:35:12

+1

这是使用UI的主要原则之一。在最短时间内阻止UI线程,在后台线程中执行您的逻辑,并使用相关准备好的数据安排UI更新。 – 2013-05-10 13:41:16

4

使用activity.runOnUiThread

Acivity.runOnUiThread(new Runnable() { 
    public void run() { 
     //something here 
    } 
}); 
0

,你必须使用一个处理程序实例,并使用POST方法都添加一个Runnable UI线程的队列里。如果你在一个活动中,你可以使用runOnUiThread这是一个“快捷方式”做同样的事情

2

你不能直接从非UI线程更新UI,但你可以使用Handler对象或AsyncTask与UI线程进行通信目的。

    的AsyncTask的
  1. .doInBackground()方法在非UI线程
  2. .onProgressUpdate()运行运行在UI线程,这样可能会改变看法
  3. 你可以使用publishProgress(:使用的AsyncTask最方便易办法)方法在doInBackground()内传递数据到.onProgressUpdate。

对不起,如果在方法名称的一些错误。 阅读http://developer.android.com/guide/components/processes-and-threads.html了解详情。

2

工作线程永远不能更新主线程,因为只有主线程可以呈现UI元素(TextView,EditText等),并且如果我们尝试更新以确保我们将发生异常。

myAcitity.runOnUiThread(new Runnable() { 
    public void run() { 
     //here you can update your UI elements because this code is going to executed by main thread 
    } 
}); 

否则,您可以使用View类

myView.post(new Runnable() { 
     public void run() { 
      //here you can update your UI elements because this code is going to executed by main thread 
     } 
    }); 
相关问题