티스토리 뷰
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mainvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/backvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<Button
android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Increase"
/>
</LinearLayout>
// ThreadRun.java
package pkg.ThreadRun;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ThreadRun extends Activity {
int mMainValue = 0;
int mBackValue = 0;
TextView mMainText;
TextView mBackText;
BackThread mThread;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainvalue);
mBackText = (TextView)findViewById(R.id.backvalue);
Button btn = (Button)findViewById(R.id.increase);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mMainValue++;
mMainText.setText("MainValue : " + mMainValue);
}
});
mThread = new BackThread(mHandler);
mThread.setDaemon(true);
mThread.start();
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if(msg.what == 0) {
mBackText.setText("BackValue : " + msg.arg1);
}
}
};
}
class BackThread extends Thread {
int mBackValue = 0;
Handler mHandler;
BackThread(Handler handler) {
mHandler = handler;
}
public void run() {
while(true){
mBackValue++;
Message msg = Message.obtain();
msg.what = 0;
msg.arg1 = mBackValue;
mHandler.sendMessage(msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e) { ; }
}
}
}
'Android > Android Studio' 카테고리의 다른 글
이미지 버튼 (0) | 2019.04.04 |
---|---|
내부 스레드 & 핸들러 (0) | 2019.04.04 |
메모 - 데이터 베이스 사용 (0) | 2019.04.04 |
ProgressDialog (0) | 2019.04.04 |
체크 박스 리스트 뷰 만들고 배경 넣기 (0) | 2019.04.04 |
- Total
- Today
- Yesterday