2017-09-25 53 views
0

我想確定我runnable在我的主線程,然後將其作爲參數傳遞到另一個線程像下面 -執行可運行的參數在自定義線程類

final Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 

       Log.i("Running runnable",""); 
      } 
     }; 

Handler handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 

     text.setText(msg.obj.toString()); 
    } 
}; 

ThreadTest handlerThreadTest = new ThreadTest(runnable, handler); 
       handlerThreadTest.start(); 

ThreadTest類定義如下 -

public class ThreadTest extends Thread { 

private Handler handler; 
private Runnable runnable; 

public ThreadTest(Runnable runnable, Handler handler) { 

    this.handler = handler; 
    this.runnable = runnable; 
} 

@Override 
public void run() { 
    super.run(); 

    runnable.run(); 

    Message msg = handler.obtainMessage(); 
    msg.obj = "YUSSSSSS!"; 
    handler.sendMessage(msg); 
} } 

我能夠發送「YUSSSSSS」消息給我的處理程序,但runnable.run()似乎並不奏效,因爲我什麼也看不到在logcat中。

我在哪裏弄錯了?

回答

1

的logcat趨於消除空消息時,某些字符添加到您的日誌,你可能會看到「運行可運行」

Log.i("RunnableImpl", "Running runnable"); 
的標記下的東西
相關問題