Created
August 27, 2011 16:21
-
-
Save ponko2/1175555 to your computer and use it in GitHub Desktop.
ScalaでAndroidアプリ作成時、AsyncTaskの可変長引数メソッドが使えないことへの対策とサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.ponko2.android.os; | |
import android.os.AsyncTask; | |
public abstract class AsyncTask1<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { | |
@Override | |
protected Result doInBackground(Params... params) { | |
return doInBackground(params.length > 0 ? params[0] : null); | |
} | |
abstract protected Result doInBackground(Params param); | |
@Override | |
protected void onProgressUpdate(Progress... values) { | |
onProgressUpdate(values.length > 0 ? values[0] : null); | |
} | |
protected void onProgressUpdate(Progress value) { | |
} | |
@SuppressWarnings({"unchecked"}) | |
protected final void publishProgress(Progress value) { | |
super.publishProgress(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.ponko2.android.sample | |
import _root_.android.app.Activity | |
import _root_.android.os.Bundle | |
import _root_.android.os.AsyncTask.Status | |
import _root_.android.widget.{TextView, Toast} | |
import _root_.android.view.Window | |
import jp.ponko2.android.os.AsyncTask1 | |
class SampleActivity extends Activity { | |
private lazy val mTask = new TestTask | |
override def onCreate(savedInstanceState: Bundle) { | |
super.onCreate(savedInstanceState) | |
val values = Seq(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
setupProgress() | |
setContentView(new TextView(this) { | |
setText(values.mkString(" + ") + " = ") | |
}) | |
mTask.execute(values) | |
} | |
override protected def onDestroy() { | |
super.onDestroy() | |
if (mTask.getStatus == Status.RUNNING) { | |
mTask cancel true | |
} | |
} | |
private def setupProgress() { | |
requestWindowFeature(Window.FEATURE_PROGRESS) | |
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS) | |
} | |
private class TestTask extends AsyncTask1[Seq[Int], Int, Int] { | |
override protected def onPreExecute() = showProgress() | |
override protected def doInBackground(values: Seq[Int]): Int = { | |
val count = values.length | |
var total = 0 | |
for (i <- 0 until count) { | |
publishProgress(((i / count.toFloat) * 10000).toInt) | |
Thread.sleep(500) | |
total += values(i) | |
} | |
total | |
} | |
override protected def onProgressUpdate(progress: Int) { | |
setProgress(progress) | |
} | |
override protected def onPostExecute(result: Int) { | |
hideProgress() | |
Toast.makeText(SampleActivity.this, result.toString, Toast.LENGTH_LONG).show() | |
} | |
private def showProgress() = { | |
setProgressBarVisibility(true) | |
setProgressBarIndeterminateVisibility(true) | |
} | |
private def hideProgress() = { | |
setProgressBarVisibility(false) | |
setProgressBarIndeterminateVisibility(false) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment