「Android/非同期処理/HandlerRunnable」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==サンプル(Runnableクラス切り分け)== MainActivity.java <pre> TextView textView = findViewById(R.id.text1); final Handler handler = new Handler(Looper.getMainLo...」) |
(→サンプル(Runnableクラス切り分け)) |
||
| 行1: | 行1: | ||
| + | 参考:https://qiita.com/8yabusa/items/f8c9bb7eb81175c49e97 | ||
| + | |||
==サンプル(Runnableクラス切り分け)== | ==サンプル(Runnableクラス切り分け)== | ||
MainActivity.java | MainActivity.java | ||
| 行34: | 行36: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | 参考:https://www.it-swarm.dev/ja/java/%E3%82%8F%E3%81%8B%E3%82%8A%E3%82%84%E3%81%99%E3%81%84%E8%AA%AC%E6%98%8E%E3%81%A7%E3%80%81java%E3%81%A7runnable%E3%81%A8%E3%81%AF%E4%BD%95%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F/1070599643/ | ||
2020年8月7日 (金) 15:58時点における版
参考:https://qiita.com/8yabusa/items/f8c9bb7eb81175c49e97
サンプル(Runnableクラス切り分け)
MainActivity.java
TextView textView = findViewById(R.id.text1);
final Handler handler = new Handler(Looper.getMainLooper());
MyRunnableTask task = new MyRunnableTask(handler, textView);
Executors.newSingleThreadExecutor().execute(task);
<pre>
MyRunnableTask.java
<pre>
import android.os.Handler;
import android.widget.TextView;
public class MyRunnableTask implements Runnable {
Handler mHandler;
TextView mTextView;
public MyRunnableTask(Handler handler, TextView textView) {
mHandler = handler;
mTextView = textView;
}
public void run() {
final String msg = "background exec";
if (mHandler != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
if (mTextView != null) {
mTextView.setText(msg);
}
}
});
}
}
}
