Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created August 15, 2013 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkAmi/6238351 to your computer and use it in GitHub Desktop.
Save thinkAmi/6238351 to your computer and use it in GitHub Desktop.
RubotoでのAsyncTaskサンプル
require 'ruboto/widget'
require 'ruboto/util/toast'
ruboto_import_widgets :Button, :LinearLayout, :TextView
java_import 'android.os.AsyncTask'
# Thread.sleepを使いたいため追加
java_import 'java.lang.Thread'
class AsyncTaskActivity
attr_accessor :status
def onCreate(bundle)
super
view =
linear_layout orientation: :vertical do
@status = text_view text: 'before', text_size: 48
button text: 'start', on_click_listener: MyClickListener.new(self)
end
self.content_view = view
end
end
class MyClickListener
def initialize(activity)
@activity = activity
end
def onClick(view)
task = MyTask.new
task.add_activity(@activity)
task.execute ''
end
end
class MyTask < AsyncTask
def add_activity(activity)
@activity = activity
end
def doInBackground(params)
Thread.sleep(1000)
'after'
end
def onPostExecute(result)
@activity.status.text = result
@activity.toast 'done!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment