今天是還滿充實的一天,雖然還是留下了很多問題,故此記錄。
第一個就是知道如何在Android中讓Activity互相傳資料,並切換到指定的Activity。程式碼如下:
==========第一個Activity==========
// 指定從A切換至B
Intent intent = new Intent();
// 啟動指定之Activity
intent.setClass(A.this, B.class);
// 將名為message的資料加入到intent內
intent.putExtra("message", "Hello SecondActivity!!!");
intent.putExtra("number1", 10);
// 儲存所需資料進入Bundle
Bundle bundle = new Bundle();
bundle.putInt("number1", 10);
bundle.putString("str1", "Hello Msg");
// 將Bundle儲存入Intent
intent.putExtras(bundle);
// 結束目前執行之Activity A.this.finish();
==========第二個Activity==========
// 取得剛剛設定的message資料
intent.getStringExtra("message");
intent.getIntExtra("number1");
// 取得Bundle資料
Bundle bundle = intent.getExtras();
再來今天最棒的進度就是把nodejs的環境架設完畢,而且一開始還以為nodejs裝好不能用,原來是因為根本下錯指令,下成node指令而非nodejs
http://www.arthurtoday.com/2011/09/ubuntu-nodejs.html#.UzTwY62SzaE
==========簡易安裝法,不是最新版本==========
// 直接安裝nodejs package
sudo apt-get install nodejs
==========完整安裝法,取得最新版本==========
// 取得最新的repository
sudo add-apt-repository ppa:chris-lea/node.js
// 自repository更新nodejs
sudo apt-get update
// 安裝最新版本的nodejs
sudo apt-get install nodejs
安裝方法二,使用nvm(node version manager)安裝:
http://www.arthurtoday.com/2011/09/ubuntu-nodejs.html#ixzz2xMMzOeNw
//安裝nvm必要工具git, c++編譯器, curl
sudo apt-get install git-core g++ curl
// 取得nvm的source並放置在~/.nvm
git clone git://github.com/creationix/nvm.git ~/.nvm
// 把nvm.sh指令載入
echo ". ~/.nvm/nvm.sh" >> ~/.bashrc
接著重新打開terminal或是輸入source~/.bashrc或. ~/.bashrc,載入新的bashrc設定到現在的bash環境中
// 安裝指定版本的nodejs, 現在最新的是v0.10.26
nvm install v0.8.9 # install current version, take some time
// 設定所有terminal預設的版本
nvm alias default v0.8.9
// 確認npm(nodejs package manager)版本
npm -v
http://andikan.github.io/blog/2012/09/24/nodejs-environment-setup/
http://www.nodebeginner.org/index-zh-tw.html
var http = require("http");
// 建立一個http監聽8888ㄟport的server
http.createServer(function(request, response) {
// 寫入html header
response.writeHead(200, {"Content-Type": "text/plain"});
// 在網頁內寫入Hello World的字串
response.write("Hello World");
// 結束
response.end();
}).listen(8888);
連接到指定的ip以及port即可顯示html網頁。
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
// 將自己定義成模組,並加入start的函數
exports.start = start;
// 將目前目錄的server.js模組引入
var server = require("./server");
// 執行剛剛在server.js當中exports.start的函數
server.start();
假如使用者的url如下,可使用url模組的函數來做url分析:
http://localhost:8888/start?foo=bar&hello=world
取得start的字串
url.parse(string).pathname
取得query的字串(foo=bar&hello=world)
url.parse(urlString).query
取得foo的資料
querystring(string)["foo"]
取得hello的資料
querystring(string)["hello"]
function route(pathname) {
console.log("About to route a request for " + pathname);
}
// router模組提供route函數
exports.route = route;
var server = require("./server");
// 引入router模組
var router = require("./router");
// 將route傳入
server.start(router.route);
// server.js
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
// 抓到url
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
再來加入router的pathname對應
// requestHandlers.js
function start() {
console.log("Request handler 'start' was called.");
}
function upload() {
console.log("Request handler 'upload' was called.");
}
exports.start = start;
exports.upload = upload;
// requestHandlers.js end
將其引入index.js中,利用javascript object的hash屬性特性來做到
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
// 設定指定的路徑,利用hash特性呼叫函數
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);
// server.js
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
// 傳入handle
route(handle, pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
// router.js
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
}
}
如此一來就可以輕鬆的利用requestHandlers做route的動作了