I experimented with scala Actors in android today.
It all works very nice, except that the actors classes are not in the scala-android library, but including the scala-library.jar and having proguard take care of removing unnecessary classes worked very well...
The actors work, only problem is that updates to Views should only be made from the main UI thread, so i added a function to allow to append Messages to TextViews by post()ing them...
Adding an click listener to a button:
// converter for anonymous func to OnClickListener implicit def func2OnClickListener(func: (View)=>Unit):OnClickListener = { new OnClickListener() { def onClick(v:View) = func.apply(v) } } button.setOnClickListener( (v:View)=>{ textView.append("click from thread: " + Thread.currentThread().getId() + "\n"); buttonActor!"msg"; scrollViewUpdate(scrollView) })
The ClickListener is outputting some code to the textView, and then sends "msg" to a actor... the Actor class and the messages used to post() functionality to the Views looks like this:
/* since views should only be accessed by the UI Thread, i use this method from ButtonActor to post the message to the TextView */ def textViewAppend(textView:TextView, msg:String) { textView.post(()=>{ textView.append(msg) () // must return Unit }) } def scrollViewUpdate(scrollView:ScrollView) { /* post function for scrollView, after updates, we want to scroll to bottom... */ scrollView.post(()=>{ scrollView.fullScroll(View.FOCUS_DOWN) () // must return Unit }) } class ButtonActor extends Actor { private val textView:TextView = findViewById(R.id.TextView01) private val scrollView:ScrollView = findViewById(R.id.ScrollView01) private val spinner:Spinner = findViewById(R.id.Spinner01) def act() { loop { receive { case msg => val textMsg = "msg received: " + msg.toString() + " from thread: " + Thread.currentThread().getId() + "\n"; textViewAppend(textView, textMsg);scrollViewUpdate(scrollView); } } } }
See it all here:
http://github.com/phueper/AndroidScalaAntTest/
Cheers, Patty