Created
August 15, 2013 04:52
-
-
Save thinkAmi/6238351 to your computer and use it in GitHub Desktop.
Rubotoでの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
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