「Android/非同期処理/HandlerRunnable」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプル(ネットワーク処理&View処理)) |
(→callback処理) |
||
(同じ利用者による、間の6版が非表示) | |||
行63: | 行63: | ||
==サンプル(ネットワーク処理&View処理)== | ==サンプル(ネットワーク処理&View処理)== | ||
ネットワーク処理はマルチスレッドで処理して、ViewはUiスレッドで処理する。 | ネットワーク処理はマルチスレッドで処理して、ViewはUiスレッドで処理する。 | ||
+ | AsyncTaskの代替になる。 | ||
+ | * doInBackground()の処理はhandler.post(new Runnable() {public void run(){}の中へ | ||
+ | * onPostExecute()の処理はuiHandler.post(new Runnable() {public void run(){}の中へ | ||
MainActivity.java | MainActivity.java | ||
行99: | 行102: | ||
try { | try { | ||
String encode = URLEncoder.encode(param1, "UTF-8"); | String encode = URLEncoder.encode(param1, "UTF-8"); | ||
− | String url = " | + | String url = "ttps://example.com/hoge?param1=" + encode; |
http.execGet(url); | http.execGet(url); | ||
Log.i("http", http.getBody()); | Log.i("http", http.getBody()); | ||
行123: | 行126: | ||
参考: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/ | 参考: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/ | ||
+ | |||
+ | ==callback処理== | ||
+ | thread処理後に元処理を呼び出す。 | ||
+ | |||
+ | MainActivity.java | ||
+ | <pre> | ||
+ | final TextView textView = findViewById(R.id.text1); | ||
+ | final Handler uiHandler = new Handler(Looper.getMainLooper()); | ||
+ | MyRunnableTask task = new MyRunnableTask(uiHandler, textView); | ||
+ | MyRunnableTask.CallbackInterface callback = new MyRunnableTask.CallbackInterface() { | ||
+ | @Override | ||
+ | public void callback() { | ||
+ | Log.i("test", "callback"); | ||
+ | } | ||
+ | }; | ||
+ | task.ThreadWithCallback(callback); | ||
+ | new Thread(task).start(); | ||
+ | </pre> | ||
+ | MyRunnableTask.java | ||
+ | <pre> | ||
+ | public class MyRunnableTask implements Runnable { | ||
+ | Handler mUiHandler; | ||
+ | TextView mTextView; | ||
+ | CallbackInterface mCallback; | ||
+ | interface CallbackInterface { | ||
+ | void callback(); | ||
+ | } | ||
+ | public MyRunnableTask(Handler handler, TextView textView) { | ||
+ | mUiHandler = handler; | ||
+ | mTextView = textView; | ||
+ | } | ||
+ | void ThreadWithCallback(CallbackInterface callback) { | ||
+ | mCallback = callback; | ||
+ | } | ||
+ | |||
+ | public void run() { | ||
+ | final String msg = "update text string"; | ||
+ | if (mUiHandler != null) { | ||
+ | mUiHandler.post(new Runnable() { | ||
+ | @Override | ||
+ | public void run() { | ||
+ | if (mTextView != null) { | ||
+ | mTextView.setText(msg); | ||
+ | } | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | mCallback.callback(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | 参考:https://peitechblog.com/?p=260 |
2020年8月17日 (月) 18:23時点における最新版
AsyncTaskはAndroid11から非推奨となったので非同期処理はこちら推奨。
サンプル
final TextView textView = findViewById(R.id.text1); final Handler handler = new Handler(Looper.getMainLooper()); new Thread(new Runnable() { @Override public void run() { final String msg = "background exec"; if (handler != null) { handler.post(new Runnable() { @Override public void run() { if (textView != null) { textView.setText(msg); } } }); } } }).start();
参考: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();
MyRunnableTask.java
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); } } }); } } }
サンプル(ネットワーク処理&View処理)
ネットワーク処理はマルチスレッドで処理して、ViewはUiスレッドで処理する。 AsyncTaskの代替になる。
- doInBackground()の処理はhandler.post(new Runnable() {public void run(){}の中へ
- onPostExecute()の処理はuiHandler.post(new Runnable() {public void run(){}の中へ
MainActivity.java
final TextView textView = findViewById(R.id.text1); final Handler uiHandler = new Handler(Looper.getMainLooper()); HandlerThread thread = new HandlerThread("other"); thread.start(); final Handler handler = new Handler(thread.getLooper()); MyRunnableTask task = new MyRunnableTask(uiHandler, handler, textView); new Thread(task).start();
MyRunnableTask.java
import android.os.Handler; import android.widget.TextView; public class MyRunnableTask implements Runnable { Handler mUiHandler; Handler mHandler; TextView mTextView; public MyRunnableTask(Handler uiHandler, Handler handler, TextView textView) { mUiHandler = uiHandler; mHandler = handler; mTextView = textView; } public void run() { final String ret = ""; if (mHandler != null) { mHandler.post(new Runnable() { @Override public void run() { final CustomHttpURLConnection http = new CustomHttpURLConnection(); try { String encode = URLEncoder.encode(param1, "UTF-8"); String url = "ttps://example.com/hoge?param1=" + encode; http.execGet(url); Log.i("http", http.getBody()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (mUiHandler != null) { mUiHandler.post(new Runnable() { @Override public void run() { if (mTextView != null) { mTextView.setText(ret); } } }); } } }); } } }
callback処理
thread処理後に元処理を呼び出す。
MainActivity.java
final TextView textView = findViewById(R.id.text1); final Handler uiHandler = new Handler(Looper.getMainLooper()); MyRunnableTask task = new MyRunnableTask(uiHandler, textView); MyRunnableTask.CallbackInterface callback = new MyRunnableTask.CallbackInterface() { @Override public void callback() { Log.i("test", "callback"); } }; task.ThreadWithCallback(callback); new Thread(task).start();
MyRunnableTask.java
public class MyRunnableTask implements Runnable { Handler mUiHandler; TextView mTextView; CallbackInterface mCallback; interface CallbackInterface { void callback(); } public MyRunnableTask(Handler handler, TextView textView) { mUiHandler = handler; mTextView = textView; } void ThreadWithCallback(CallbackInterface callback) { mCallback = callback; } public void run() { final String msg = "update text string"; if (mUiHandler != null) { mUiHandler.post(new Runnable() { @Override public void run() { if (mTextView != null) { mTextView.setText(msg); } } }); } mCallback.callback(); } }