Adobe董龙飞:PhoneGap 15Min.ppt
PhoneGap,Mark DongDeveloper E,始于2008年 旧金山的iPhoneDevCamp“bridging the gap between the web and the iphone SDK”PhoneGap:its like AIR for the Iphone(2008-9-18,Nitobi blog),1.3 Released!,CallBack,更名为 Apache Cordova,Native Web Control,全功能浏览器没有边框,JS,Native,PhoneGap plugin,HTML5项目,phonegap.jar,plugins.xml,AndroidManifest.xml,android activity,In Java,public class CirclesActivity extends DroidGap Override public void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);super.loadUrl(file:/android_asset/www/index.html);,Native Wrapper with PhoneGap,public class callsPGPlugin extends Plugin/List Actionspublic static final String ACTION=list;Overridepublic PluginResult execute(String action,JSONArray data,String callbackId)PluginResult result=null;if(ACTION.equals(action)CallLogAI callLogAI=new CallLogAI(ctx);JSONObject callsHistory=callLogAI.fetchCallLogs(null);Log.d(RESULT=,callsHistory.toString();result=new PluginResult(Status.OK,callsHistory);elseresult=new PluginResult(Status.INVALID_ACTION);Log.d(CallsPlugin,Invalidate action:+action);return result;,Java定制plugin,注册plugin,In JavaScript,Javascript接口,var CallsListing=function();CallsListing.prototype.list=function(successCallback,failureCallback)return PhoneGap.exec(successCallback,failureCallback,CallsHistoryPlugin,list,test);PhoneGap.addConstructor(function()PhoneGap.addPlugin(callsListing,new CallsListing););,window.plugins.callsListing.list(function(r)printResult(r),function(e)console.log(e);,Javascript 调用plugin,小结,window.plugins.callsListing.list(function(r)printResult(r),function(e)console.log(e);,CallsListing.prototype.list=function(successCallback,failureCallback)return PhoneGap.exec(successCallback,failureCallback,CallsHistoryPlugin,list,test);,public class callsPGPlugin extends Plugin/List Actionspublic static final String ACTION=list;Overridepublic PluginResult execute(String action,JSONArray data,String callbackId),Android,CallbackServer:XmlHttpRequestsever,WebChromClient:onJsPrompt,addJavaScriptInterface,CallbackServer:XmlHttpRequestsever,WebChromClient:覆盖onJsPrompt,From JS to Native,From Native to JS,CallsListing.prototype.list=function(successCallback,failureCallback)return PhoneGap.exec(successCallback,failureCallback,CallsHistoryPlugin,list,test);,PhoneGap.exec=function(success,fail,service,action,args)var r=prompt(JSON.stringify(args),gap:+JSON.stringify(service,action,callbackId,true);,Droidgap.javapublic boolean onJsPrompt(WebView view,String url,String message,String defaultValue,JsPromptResult result)String r=pluginManager.exec(service,action,callbackId,message,async);,Pluginmanager.javapublic String exec(final String service,final String action,final String callbackId,final String jsonArgs,final boolean async)cr=plugin.execute(action,args,callbackId);ctx.sendJavascript(cr.toErrorCallbackString(callbackId);,Pluginmanager.javactx.sendJavascript(cr.toErrorCallbackString(callbackId);,CallbackServer.java:XHR server,PhoneGap.Channel.join(function()/Start listening for XHR callbacks setTimeout(function()if(PhoneGap.UsePolling)PhoneGap.JSCallbackPolling();,IOS,JS到Native的通讯,document.location=“gap:/Class.method/args”,Native到JS的通讯,UIWebView.stringByEvaluatingJavaScriptFromString,webView=UIWebView alloc initWithFrame:webViewBounds;,实例化UIWebView,Mark DongDeveloper E,