2009年11月15日 星期日

Android 感應器 - Sensor.TYPE_ORIENTATION

在Activity的onCreate
SensorManager sm = (SensorManager)getSystemService(SENSOR_SERVICE);
List sensors = sm.getSensorList(Sensor.TYPE_ORIENTATION);
if (sensors.size()==1) {
sensor = sensors.get(0);
}
在onResume註冊一個SensorEventListener
if (sensor!=null) sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
在onStop 移除SensorEventListener的註冊
if (sensor!=null) sm.unregisterListener(this);

在SensorEventListener中public void onSensorChanged(SensorEvent event)
可得三個方向傳感器傳來的值
event.values[SensorManager.DATA_X]
代表手機平放時, 手機與北方的角度(0-360)
event.values[SensorManager.DATA_Y]
代表手機平放時的上下傾斜角度
event.values[SensorManager.DATA_Z]
代表手機平放時的左右傾斜角度


2009年6月8日 星期一

Rails 上載文件

(1)建立一個rails project
rails uploaddemo -d sqlite3

(2)在uploaddemo/public, 建立一個文件夾data, 用來保存上載的文件

(3)創建一個controller名為upload, 包含兩個action, 分別為selectfile和savefile
selectfile用來選擇文件, savefile用來保存文件
script/generate controller upload selectfile savefile

(4)編寫selectfile.html.erb
<% form_for :datafile, :url=>{:action=>:uploadFile}, :html=>{:multipart=>true} do |form| %>
<%=form.file_field 'uploaded_data' %>
<%=submit_tag 'upload file' %>
<% end %>

(5)編寫controller的savefile
filename = params[:datafile][:uploaded_data].original_filename
File.open("#{RAILS_ROOT}/public/data/#{filename}", "wb") do |file|
file.write(params[:datafile][:uploaded_data].read)
end

完整UploadController為
class UploadController < filename =" params[:datafile][:uploaded_data].original_filename" text="">"File Upload Successed!"
end
end

(6)登入http://localhost:3000/upload/selectfile, 選擇文件和上載

(7)若上載成功, 頁面會轉到http://localhost:3000/upload/savefile
並在畫面見到 File Upload Successed!字眼

(8)檢查uploaddemo/public/data會見到你剛上載的文件

註: 若文件名為中文會有問題, 解決中...

2009年5月13日 星期三

Ruby 1 -- 安裝

在windows下的安裝Ruby

(1)在http://rubyforge.org/projects/rubyinstaller/下載最新的 One-Click Ruby Install
如:http://rubyforge.org/frs/download.php/47082/ruby186-27_rc2.exe

(2)安裝 ruby186-27_rc2.exe

(3)安裝完之後,在cmd下鍵入ruby -v
會出現你所安裝的ruby版本信息如下所示,恭喜你,你已成功安裝ruby
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]