add Timer sample app for android

This commit is contained in:
Laurent Sansonetti
2014-04-26 01:22:34 +02:00
parent 089ec1c75e
commit a7b1867cfb
3 changed files with 92 additions and 0 deletions

13
android-samples/Timer/.gitignore vendored Normal file
View File

@@ -0,0 +1,13 @@
.repl_history
build
tags
.DS_Store
nbproject
.redcar
#*#
*~
*.sw[po]
.eprj
.sass-cache
.idea
.dat*.*

View File

@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
$:.unshift("../..//lib")
require 'motion/project/template/android'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'Timer'
app.api_version = '18'
app.sdk_path = File.expand_path('~/src/android-sdk-macosx')
app.ndk_path = File.expand_path('~/src/android-ndk-r9d')
end

View File

@@ -0,0 +1,68 @@
class TimerButtonListener < Java::Lang::Object
def set_activity(activity)
@activity = activity
end
def onClick(view)
@activity.toggleTimer
end
end
class TimerTask < Java::Util::TimerTask
def set_activity(activity)
@activity = activity
end
def run
@activity.handler.post -> { @activity.updateTimer }
end
end
class MainActivity < Android::App::Activity
def onCreate(savedInstanceState)
super
@handler = Android::Os::Handler.new
layout = Android::Widget::LinearLayout.new(self)
layout.orientation = Android::Widget::LinearLayout::VERTICAL
@label = Android::Widget::TextView.new(self)
@label.text = 'Tap to start'
@label.textSize = 40.0
@label.gravity = Android::View::Gravity::CENTER_HORIZONTAL
layout.addView(@label)
@button = Android::Widget::Button.new(self)
@button.text = 'Start'
listener = TimerButtonListener.new
listener.set_activity self
@button.onClickListener = listener
layout.addView(@button)
self.contentView = layout
end
def handler
@handler
end
def toggleTimer
if @timer
@timer.cancel
@timer = nil
@button.text = 'Start'
else
@timer = Java::Util::Timer.new
@counter = 0
task = TimerTask.new
task.set_activity self
@timer.schedule task , 0, 100
@button.text = 'Stop'
end
end
def updateTimer
@label.text = "%.1f" % (@counter += 0.1)
end
end