Cordova / PhoneGap开发入门,基于HTML、CSS和JavaScript的Android移动开发框架

一 、Cordova 简介:

1. 基于HTML,CSS和JavaScript的移动开发框架
2. 跨平台快速开发:能够支持iPhone,Android,Palm,Symbian, WP7, WP8, Bada和Blackberry
3. 核心功能:包括地理定位,加速器,联系人,声音和振动等
4. 拥有丰富的插件,可以以此扩展无限的功能

二、历史由来

1. PhoneGap:http://phonegap.com    由一个叫Nitobi 的小公司发家,后被Adobe公司收购。

2. Cordova:http://cordova.apache.org   Adobe后来将PhoneGap核心代码贡献给Apache并进行开源,重新取名Cordova。因为 Nitobi 的办公地点曾设在在一条叫Cordova 的街道上。

三 、PhoneGap/Cordova 系统架构图

    

四 、Cordova 类图

    

核心Java 类:

1. CordovaPlugin.java
   所有插件的父类,主要实现execute()
2. PluginManager.java
   负责插件的加载、初始化、和事件的分发调用
3. CordovaInterface.java
   Activity 与插件之间的接口
4. CordovaWebView.java
   对WebView 进行封装,接口类,提供Java Native 与JS之间的通信
5. SystemExposedJsApi.java / CordovaBridge.java
   Java 与 JS 之间的调用API

五、系统逻辑流程图

   

核心JavaScript 文件:

1. cordova.js
   框架核心JS文件,实现JS框架消息的通信机制
2. exec.js
   各种函数定义、声明
3. cordova_plugins.js 
   插件注册表
4. customerPlugin.js
   用户自己定义的JS文件

六、搭建开发环境(Mac电脑):

1. 首先安装 node.js

   自2.9.0以后,不提供直接下载,需要用来Cordova命令行工具,Cordova命令行工具运行于Node环境. 安装Node 我们需要用到 homebrew

2. 安装homebrew 工具

   $ ruby -e "$(curl -fsSkL https://raw.github.com/Homebrew/homebrew/go/install)”
   $ brew install node   该命令执行后,自动装好node和npm

3. 安装Cordova

  $ npm install -g cordova  该命令执行后,cordova 就你的电脑上安装好了。可以通过“$ cordova -v” 查看是否安装成功:

 

七、创建Cordova 工程

1. 创建工程目录

    $ cordova create <project-path> 执行完之后,就自动创建出如下目录

   

2. 进入到工程目录,创建移动平台工程文件

   $ cd <project-path>

   $ cordova platform  查看所有支持的平台

    

   $ cordova platform add android 创建android 平台对应的工程文件,执行该命令后,会自动生成一个/platform/android 工程目录

   

3. 将platform下面的android 工程导入到eclipse,在eclipse中工程目录结构如下: 

   

4. Eclipse 编译工程,安装到手机执行,发现一个 helloworld 的 HTML页面(index.html),啥东东也没有:

    

八、快速开发

Cordova 提供了CordovaActivity 、 CordovaInterface 和CordovaPlugin 让Android 开发者快速开发:

1. 首选 MyActivity直接继承类 CordovaActivity

2. 或者MyActivity自己实现CordovaInterface 接口

3. 用户继承CordovaPlugin, 实现自定义插件

4. 每个实现了CordovaInterface接口的Activity都对应一套独立的WebView,CordovaPlugin,PluginManager,没有共享的

九、插件扩展举例:Camera

Cordova 已经实现了丰富的插件,开发着可以直接安装已有的插件,扩展自己App功能。当然也可以自定义插件,只需要仿照已有的插件进行开发。

1. 安装Camera 插件

    

2. 安装完后,会在原有的android 工程下自动生成Camera插件文件,如下图所示:

       

3. 首先在helloworld的页面(index.html)里添加一个Camera测试页面的跳转链接:<a href="file:///android_asset/www/camera.html">Test Camera</a>

<!DOCTYPE html>
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
    <head>
        <!--
        Customize this policy to fit your own app's needs. For more guidance, see:
            https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
        Some notes:
            * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
            * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
            * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
                * Enable inline JS: add 'unsafe-inline' to default-src
        -->
<!--         <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 style="margin-top:50px">
                <a href="file:///android_asset/www/camera.html">Test Camera</a>
            </div>
        </div>
        
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            document.getElementById("myBtn").οnclick=function(){ testCamera() };
           
            function testCamera() {
               navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
            }
            function onSuccess(imageURI) {
                var image = document.getElementById("myTestImage");
                image.src = imageURI;
            }

            function onFail(message) {
                alert('Failed because: ' + message);
            }
        </script>
    </body>
</html>

  

4. 然后编写Camera 的测试的页面 camera.html

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

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

    <script type="text/javascript">
        var pictureSource;  //设定图片来源
        var destinationType; //选择返回数据的格式
    
        document.addEventListener("deviceready",onDeviceReady,false);
    
        // Cordova准备好了可以使用了
        function onDeviceReady() {
            pictureSource=navigator.camera.PictureSourceType;
            destinationType=navigator.camera.DestinationType;
        }
    
        function onPhotoDataSuccess(imageData) {
          // base64 encoded image data
          var smallImage = document.getElementById('smallImage');
          smallImage.style.display = 'block';
          //在使用base64编码的时候需要使用这样的前缀
          smallImage.src = "data:image/jpeg;base64," + imageData;
        }
    
        // Called when a photo is successfully retrieved
        //
        function onPhotoURISuccess(imageURI) {
          // image file URI 
          var largeImage = document.getElementById('largeImage');
          largeImage.style.display = 'block';
          //使用image file URI 直接赋值就可以了
          largeImage.src = imageURI;
        }
    
        // 第一个按钮调用函数
        function capturePhoto() {
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
            destinationType: destinationType.DATA_URL, sourceType: pictureSource.CAMERA  });
        }
    
        //第二个按钮调用的函数
        function capturePhotoEdit() {
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
            destinationType: destinationType.DATA_URL, sourceType: pictureSource.CAMERA  });
        }
    
        //第三/四个按钮调用的函数
        function getPhoto(source) {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
            destinationType: destinationType.FILE_URI, sourceType: source });
        }
    
        function onFail(message) {
          alert('Failed because: ' + message);
        }
    </script>
  </head>
  
  <body>
    <h1 style="margin-top:10px;text-align:center">Camera HTML Page</h1> 
    <button style="margin-top:10px" οnclick="capturePhoto();">Capture Photo</button> <br>
    <button style="margin-top:10px" οnclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
    <button style="margin-top:10px" οnclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
    <button style="margin-top:10px" οnclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
    <img style="display:none;width:200px;height:200px;margin-top:10px" id="smallImage" src="" />
    <img style="display:none;width:200px;height:200px;margin-top:10px" id="largeImage" src="" />
  </body>
</html>

测试页面的四个button,分别调用不同的camera 接口,具体的接口定义可以参考 API Reference:https://www.npmjs.com/package/cordova-plugin-camera  

  getPicture(onSuccess, onFail, { quality: 50, destinationType: DATA_URL});
  getPicture(onSuccess, onFail, { quality: 50, destinationType: DATA_URL, allowEdit : true});
  getPicture(onSuccess, onFail, { quality: 50, destinationType: FILE_URL,  source:PHOTOLIBRARY });
  getPicture(onSuccess, onFail, { quality: 50, destinationType: FILE_URL,  source:SAVEDPHOTOALBUM });

5. Demo 演示


 Demo 源代码下载地址 http://download.csdn.net/download/wangbaochu/9413176

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值