Android/非同期処理/HandlerRunnable
提供: 初心者エンジニアの簡易メモ
参考:https://qiita.com/8yabusa/items/f8c9bb7eb81175c49e97
サンプル(Runnableクラス切り分け)
MainActivity.java
final TextView textView = findViewById(R.id.text1); final Handler handler = new Handler(Looper.getMainLooper()); MyRunnableTask task = new MyRunnableTask(handler, textView); new Thread(task).start(); <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); } } }); } } }