Cordova入门系列(二)分析第一个helloworld项目

上一章我们介绍了如何创建一个cordova android项目,这章我们介绍一下创建的那个helloworld项目的代码,分析其运行。

MainActivity.java

我们已经将MainActivity导入到了eclipse中。打开scr下com.example.hello下的MainActivity.java。

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">//MainActivity继承了CordovaActivity
public class MainActivity extends CordovaActivity
{
@Override public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState); // Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}</pre>
  学过安卓的都知道,Activity在启动的时候会首先调用onCreate方法。

loadUrl(launchUrl);会在当前的WebView中去加载首页,当然这个首页是我们自己配置的,在res/xml/config.xml中。<contentsrc="index.html"/>。这个路径都是指的assets/www下的路径。

这样这个app启动的时候会首先调用这个MainActivity(当然这是在AndroidManifest.xml中配置的),然后Activity启动的时候会将index.html加载在其WebView中,然后我们就看到了Cordova的页面。

index.html

我们再来看看index.html中都有什么内容。

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"><html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>  </pre>

<script type="text/javascript" src="cordova.js"></script>

<script type="text/javascript" src="js/index.js"></script>

在显示这个index页面的时候,会加载两个js文件。cordova.js就是cordova的api,调用原生内容用的,相当于jar包。

index.js

再看这个index.js。
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">var app = { // 初始化方法,会调用绑定事件的方法
initialize: function() { this.bindEvents();
}, // 绑定事件的方法,事件可以是这些:
// 'load', 'deviceready', 'offline', and 'online'.
//该事件是在 cordova 载入完成后发生的事件,它表示cordova 加载完成并准备访问,yourCallbackFunction,相当于程序的入口功能
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
}, // deviceready 后事件处理
onDeviceReady: function() {
app.receivedEvent('deviceready');
}, // 事件处理,更新DOM,改变两个元素的css
receivedEvent: function(id) { var parentElement = document.getElementById(id); var listeningElement = parentElement.querySelector('.listening'); var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}

}; //调用初始化方法
app.initialize();</pre>

加载index.js之后,程序会首先调用初始化方法,初始化的时候会去绑定事件监听:document.addEventListener('deviceready', this.onDeviceReady, false);

deviceready事件,该事件是在 cordova 载入完成后发生的事件,它表示cordova 加载完成并准备访问,你传进去的回调函数,相当于程序的入口函数。

当cordova准备好的时候,会去首先执行你传进去的回调函数,这里是onDeviceReady()。这个方法会改变DOM元素的css样式。

最终效果就是,当cordova没有加载完的时候,页面上Apache Cordova下面那里显示的是:Connecting to Device(背景是灰色)。加载完之后,cordova准备好之后,原来显示的消失,现在显示的是:Device is Ready(背景是绿色)

转自:http://www.cnblogs.com/lishuxue/p/6015420.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,523评论 0 17
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,270评论 25 708
  • 创建第一个Helloworld项目,现在来分析运行机制 MainActivity.java   我们已经将Main...
    samychen阅读 1,144评论 0 0
  • 据说在北方的大部分地区,夜晚超过8点,就极少有人上街了,大部分店铺在那个钟点也都已经打烊。我想,北方夜晚比较冷可能...
    黄大醒阅读 196评论 0 1
  • 伴着清晨凉爽的风,社会主义核心价值观宣讲实践队开始了郑州博物馆之行。今天是实践活动的第五天,但队员们依旧热情满满,...
    慊殇阅读 247评论 0 0