Skip to content

Instantly share code, notes, and snippets.

@wicksome
Created August 21, 2014 07:25
Show Gist options
  • Save wicksome/4183bc4821c00634f310 to your computer and use it in GitHub Desktop.
Save wicksome/4183bc4821c00634f310 to your computer and use it in GitHub Desktop.
handler_1
public class ThreadHandlerTest extends Activity {
TextView tvTest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tvTest = (TextView) findViewById(R.id.tv_test);
BackThread thread = new BackThread();
thread.setDaemon(true); // 메인스레드와 같이 종료하기 위해서
thread.start();
}
class BackThread extends Thread {
@Override
public void run() {
val++;
mHandler.sendEmptyMessage(0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 스레드간의 통신장치
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0) {
tvTest.setText(val + "");
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment