AsyncTaskã®ãµã³ãã«
androidã§ãµã¼ãã¢ã¯ã»ã¹ãªã©éãã®å¦çãå®è¡ããå ´åã«ã¯AsyncTaskãªã©ã§éåæã«è¡ãã
ãã®ã¨ããActivityã¨ã»ããã§AsyncTaskãæ¸ãããããå ´åãããããããããActivityã¨AsyncTaskã¯çåæéãéãã
è£ã§AsyncTaskãåãã¦ããã¨ãã«è¡¨ã§ãµã¯ãµã¯ã¨Activityãåãæ¿ããAsyncTaskãçµãã£ãæã«ãã¾ãã¾åãã¦ããActivityã§ä½ãå¦çããããå ´åãããã
ããããå¦çã¯Serviceã§ããã¹ãã¨ããåãããããããããªãããã²ã¨ã¾ãAsyncTaskã§ãããªããããªæãã§ã¯ãªããã¨ããã®ãæãã¦ã¿ãã
AsyncTaskå
ã§ProgressDialogãåºãå ´åãããã®æ©æ§ã«onPauseã®ã¿ã¤ãã³ã°ã§dismissããé¨åãçµã¿è¾¼ããã¨ã§ãView not attached to window managerãã¨ãè¨ãããªãã¦æ¸ãããã«ãªãã¯ãã
Applicationã«Activityã®ã¤ã³ã¹ã¿ã³ã¹ãä¿æããã®ã¯ãæ³åº¦ã ããonResumeã§ç»é²/onPauseã§è§£é¤ãèªåã§ãã£ã¦ãããã°ç¡åé¡ã
å½ããåã£ã¡ãããå½ããåã®ã³ã¼ãã§ã¯ããã¾ãããèªåã¸ã®ã¡ã¢çã«ã
- ãµã³ãã«ã³ã¼ãå ¨ä½ï¼AsyncSample.zip
- AsyncTaskãçµãã£ããã¨ãéç¥ãã¦ãããã¤ã³ã¿ã¼ãã§ã¼ã¹ãActivityã«å®è£ ãããã
package info.unknown_artifacts.example.asyncsample; public interface AsyncTaskCompleteListener { void onAsyncTaskComplete(AsyncTaskResult<?> result); }
- ä¸è¨ãªã¹ãã¼ãä¿æããã¤ã³ã¿ã¼ãã§ã¼ã¹ãAsyncTaskã«å®è£ ãããã
package info.unknown_artifacts.example.asyncsample; public interface AsyncTaskNotifier { AsyncTaskCompleteListener getListener(); void setListener(AsyncTaskCompleteListener listener); }
- AsyncTaskãçµãã£ãã¨ãã«ãªã¹ãã¼ã«æ¸¡ãæ å ±ãé©å½ãããã«ãã»ã©ãããã
package info.unknown_artifacts.example.asyncsample; public class AsyncTaskResult<T> { private String id = null; private T val = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public T getVal() { return val; } public void setVal(T val) { this.val = val; } }
- ApplicationãAsyncTaskNotifierï¼å®ä½ã¯AsyncTaskï¼ãä¿æããã
package info.unknown_artifacts.example.asyncsample; import android.app.Application; public class SampleApplication extends Application { // å®éã«ã¯AsyncTaskNotifierãã«ãã¼ãinterfaceåãã¹ãã ãææã private AsyncTaskNotifier task = null; public void setTask(AsyncTaskNotifier task) { this.task = task; } public void setAsyncTaskCompleteListener(AsyncTaskCompleteListener listener) { if (task != null) { task.setListener(listener); } } }
- ãµã³ãã«ã®AsyncTaskã
package info.unknown_artifacts.example.asyncsample; import android.os.AsyncTask; public class SampleAsyncTask extends AsyncTask<Long, Void, String> implements AsyncTaskNotifier { private AsyncTaskCompleteListener listener = null; public AsyncTaskCompleteListener getListener() { return listener; } public void setListener(AsyncTaskCompleteListener listener) { this.listener = listener; } @Override protected String doInBackground(Long... params) { try { // æéã®ãããå¦çã ã¨æãããã Thread.sleep(params[0]); } catch (InterruptedException e) { /* N/A */ } return params[0].toString(); } @Override protected void onPostExecute(String val) { AsyncTaskResult<String> result = new AsyncTaskResult<String>(); result.setId(getClass().getName()); result.setVal(val); if (listener != null) { listener.onAsyncTaskComplete(result); } } public static void startSampleAsyncTask(SampleApplication app) { SampleAsyncTask task = new SampleAsyncTask(); app.setTask(task); task.execute(6000l); } }
- ãµã³ãã«ã®ActivityãApplicationçµç±ã§ã®AsyncTaskã¸ã®èªèº«ã®ç»é²/解é¤ã¯ããå°ããããã«ããæ¹ãããããã
package info.unknown_artifacts.example.asyncsample; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class A01Activity extends Activity implements AsyncTaskCompleteListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a01); ((Button) findViewById(R.id.btn_a02)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(A01Activity.this, A02Activity.class); startActivity(intent); } }); ((Button) findViewById(R.id.btn_start_task)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SampleApplication sampleApplication = (SampleApplication) getApplication(); SampleAsyncTask.startSampleAsyncTask(sampleApplication); sampleApplication.setAsyncTaskCompleteListener(A01Activity.this); } }); } @Override protected void onResume() { super.onResume(); ((SampleApplication) getApplication()).setAsyncTaskCompleteListener(this); } @Override protected void onPause() { super.onPause(); ((SampleApplication) getApplication()).setAsyncTaskCompleteListener(null); } @Override public void onAsyncTaskComplete(AsyncTaskResult<?> result) { ((SampleApplication) getApplication()).setTask(null); // åä¸ã§ããå ´åã¯<String>決ããã¡ããã¤ãgetIdã®åå²ã¯çãã¦ãã if (result.getId().equals(SampleAsyncTask.class.getName())) { ((TextView) findViewById(R.id.lbl_message)).setText("[" + (String) result.getVal() + "]"); } } }