簡単なThreadサンプル
ボタンを押すとThread.sleepで0.2秒待機後カウントを開始します。再度ボタンを押すとカウントを終了します。
public class MainActivity extends AppCompatActivity implements Runnable {
Thread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void button1_onClick(View view) {
if (thread == null) {
thread = new Thread(this);
thread.start();
} else {
thread = null;
}
}
public void run() {
int i = 0;
while (thread != null) {
Log.i("i = ", Integer.toString(i));
i++;
try {
Thread.sleep(200);
} catch (Exception ex) {
}
}
}
}
