「Android/マルチスレッド」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプルソース) |
|||
| (同じ利用者による、間の4版が非表示) | |||
| 行14: | 行14: | ||
new Thread(new Runnable() { | new Thread(new Runnable() { | ||
// マルチスレッド内でtoastなどを使う場合はこちらに記述 | // マルチスレッド内でtoastなどを使う場合はこちらに記述 | ||
| − | runOnUiThread(new Runnable() { | + | activity.runOnUiThread(new Runnable() { |
public void run() { | public void run() { | ||
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show(); | Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show(); | ||
| 行20: | 行20: | ||
}); | }); | ||
}).start(); | }).start(); | ||
| + | |||
| + | ==秒カウント処理== | ||
| + | |||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); | ||
| + | setContentView(R.layout.activity_main); | ||
| + | Button btn = (Button)findViewById(R.id.button01); | ||
| + | btn.setOnClickListener(new View.OnClickListener() { | ||
| + | public void onClick(View v) { | ||
| + | ThreadSample thread = new ThreadSample(); | ||
| + | thread.start(); | ||
| + | Log.i("threadexec", "ThreadSample処理開始"); | ||
| + | try { | ||
| + | // thread.join(); // Threadが終わるまで待機(途中で別のクリックイベントを処理させても処理されなくなってしまうのでおすすめしない) | ||
| + | thread.join(100); // 処理開始から0.1秒間待機だけ | ||
| + | } catch (InterruptedException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | Log.i("threadexec", "ThreadSample処理終了"); | ||
| + | } | ||
| + | }); | ||
| + | Button btnCheck = (Button)findViewById(R.id.button02); | ||
| + | btnCheck.setOnClickListener(new View.OnClickListener() { | ||
| + | public void onClick(View v) { | ||
| + | Log.i("threadexec", "isAlive=" + thread.isAlive()); | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | public class ThreadSample extends Thread { | ||
| + | public void run(){ | ||
| + | for (int i = 0; i < 5; i++) { | ||
| + | try { | ||
| + | Thread.sleep(1000); // 1秒待機 | ||
| + | Log.i("threadexec", (i + 1) + "秒経過"); | ||
| + | } catch (InterruptedException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | レイアウト | ||
| + | <Button | ||
| + | android:text="Button" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:id="@+id/button01"> | ||
| + | </Button> | ||
| + | |||
| + | ==関連== | ||
| + | [[Android/handler]] [ショートカット] | ||
2019年9月11日 (水) 18:50時点における最新版
サンプルソース
// マルチスレッドロード
new Thread(new Runnable() {
@Override
public void run() {
// ここにロジックを書く
}
}).start();
サンプルソース(toastなどを使う場合)
// マルチスレッドロード
new Thread(new Runnable() {
// マルチスレッド内でtoastなどを使う場合はこちらに記述
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
}
});
}).start();
秒カウント処理
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.button01);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ThreadSample thread = new ThreadSample();
thread.start();
Log.i("threadexec", "ThreadSample処理開始");
try {
// thread.join(); // Threadが終わるまで待機(途中で別のクリックイベントを処理させても処理されなくなってしまうのでおすすめしない)
thread.join(100); // 処理開始から0.1秒間待機だけ
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("threadexec", "ThreadSample処理終了");
}
});
Button btnCheck = (Button)findViewById(R.id.button02);
btnCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("threadexec", "isAlive=" + thread.isAlive());
}
});
}
public class ThreadSample extends Thread {
public void run(){
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000); // 1秒待機
Log.i("threadexec", (i + 1) + "秒経過");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
レイアウト
<Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button01"> </Button>
関連
Android/handler [ショートカット]
