app開發 - web developer的逆襲

34
Web Developer 的逆襲 Cordova (Phonegap) Plugin 加碼演出串接 Parse 2014/03/22 Marty

Upload: -

Post on 14-May-2015

769 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: App開發 - Web Developer的逆襲

Web Developer 的逆襲Cordova (Phonegap) Plugin

加碼演出串接 Parse

2014/03/22 Marty

Page 2: App開發 - Web Developer的逆襲

今天玩什麼?

1. Cordova CLI (create、add android、build)

2. GenyMotion (run Android app)

3. Install Cordova Plugin & Trace (camera)

4. How to write a Cordova Plugin5. BaaS (Parse)

Page 3: App開發 - Web Developer的逆襲

Web 經由 plugin 獲取超能力

1. Sensor2. Native API3. MultiThread4. Connect anywhere Wifi(Http、TCP/IP)、Bluetooth5. ...

Page 4: App開發 - Web Developer的逆襲

環境要先安裝好

● Android SDK● NodeJS●● GenyMotion

npm install cordova

Page 5: App開發 - Web Developer的逆襲

cordova create hello com.example.hello HelloWorld

Create Android Project

目錄名稱

package Name

App Name

Step1:

cd helloStep2:

cordova platform add androidStep3:

$

$

$

Page 6: App開發 - Web Developer的逆襲

WebView - 先天限制!

Page 7: App開發 - Web Developer的逆襲

Android activity

Page 8: App開發 - Web Developer的逆襲

install APK

cordova build$

cd platforms/android/ant-build$

adb install -r HelloWorld-debug.apk$

Step1:

Step2:

Step3:

Page 9: App開發 - Web Developer的逆襲

沒有Android手機 ?

Page 10: App開發 - Web Developer的逆襲

來來來,裝plugin

cordova plugin add org.apache.cordova.camera$

Page 11: App開發 - Web Developer的逆襲

用 JQMDesigner 設計畫面,匯出 html匯出HTML

Page 12: App開發 - Web Developer的逆襲

更新 html 檔案 $project/www/index.html

改成用本地的

Page 13: App開發 - Web Developer的逆襲

install APK - Camera

cordova build$

cd platforms/android/ant-build$

adb install -r HelloWorld-debug.apk$

Step1:

Step2:

Step3:

Page 16: App開發 - Web Developer的逆襲

JavaScript invoke Java's Method

cordova.exec(function(winParam) {}, function(error) {}, "service", "action", ["firstArgument", "secondArgument", 42,false]);

@Overridepublic boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("beep".equals(action)) { this.beep(args.getLong( 0)); callbackContext.success(); return true; } return false; // "MethodNotFound" error.}

JavaScript

Java

Page 17: App開發 - Web Developer的逆襲

Success callback

cordova.exec(function(winParam) {}, function(error) {}, "service", "action", ["firstArgument", "secondArgument", 42,false]);

@Overridepublic boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("beep".equals(action)) { this.beep(args.getLong( 0)); callbackContext.success(); return true; } return false; // "MethodNotFound" error.}

JavaScript

Java

Page 18: App開發 - Web Developer的逆襲

Failure callback

cordova.exec(function(winParam) {}, function(error) {}, "service", "action", ["firstArgument", "secondArgument", 42,false]);

@Overridepublic boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("beep".equals(action)) { this.beep(args.getLong( 0)); callbackContext.success(); return true; } return false; // "MethodNotFound" error.}

JavaScript

Java

Page 19: App開發 - Web Developer的逆襲

getActivity().runOnUiThread(...)

@Overridepublic boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { if ("beep".equals(action)) { final long duration = args.getLong(0); cordova.getActivity().runOnUiThread(new Runnable() { public void run() { ... callbackContext.success(); // Thread-safe. } }); return true; } return false;}

與UI互動的事,通知UiThread去安排執行

Page 20: App開發 - Web Developer的逆襲

getThreadPool().execute(...)

@Overridepublic boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { if ("beep".equals(action)) { final long duration = args.getLong(0); cordova.getThreadPool().execute(new Runnable() { public void run() { ... callbackContext.success(); // Thread-safe. } }); return true; } return false;}

在ThreadPool做事,別阻礙WebCore Thread

Page 21: App開發 - Web Developer的逆襲

Camera code trace

Page 22: App開發 - Web Developer的逆襲

trace camera

Page 23: App開發 - Web Developer的逆襲

www/plugins/...camera/www/Camera.js

Page 24: App開發 - Web Developer的逆襲

org.apache.cordova.camera.CameraLauncher

同時只能用一個相機?

Page 26: App開發 - Web Developer的逆襲

先宣告,ServiceName、實作類別

記得 ServiceName、實作類別(含Class)

ServiceName package.

Class

Page 27: App開發 - Web Developer的逆襲

新增Echo類別

Copy & Read,了解參數傳遞和用法

繼承

action

找不到方法

回呼成功 & 失敗方法

Page 28: App開發 - Web Developer的逆襲

在JS使用 cordova.exec(...)方法

一個plugin (Service) 可以有多種Action,Action攜帶JSONArray

exec(<successFunction>, <failFunction>, <service>, <action>, [<args>]);$

Page 29: App開發 - Web Developer的逆襲

config.xml

index.html

Echo.java

Page 30: App開發 - Web Developer的逆襲

完成 Cordova plugin

Page 31: App開發 - Web Developer的逆襲

BaaS (Backend as a Service)

Page 32: App開發 - Web Developer的逆襲

先試試存資料...

Page 33: App開發 - Web Developer的逆襲

用 Web API 串接 (Parse)

http://www.parsecdn.com/js/parse-1.2.18.min.js