android phonegap插件开发方法 plugin

此篇文章以cordova 3.4版本编写

phonegap的插件开发 与javascript调用android的Activity功能,以及相互传递数据.

本节讲的是 自主编写 phonegap插件提供下载

 

据我总结核心步骤: 创建工程 ; 编写插件 ;编译工程; 调用插件;

 

按照如下步骤就能生产出代码:

打开cmd 控制台

1 使用命令行 建立phonegap工程

2 将工程导入 eclipse

3 在assents 目录下的 cordova-plugins.js文件添加配置

4 在plugin目录下编写javascript接口

5 在res/xml 目录下配置 config.xml 文件

6 在src目录下编写java文件

 

最后在javascript文件中调用接口

 

总体说 主要是后4个步骤详细分解讲解

 

 

 

接下来给大家分解演示:

目测大家都是已经安装好环境的,如果没有搭好环境 可以查看我的phonegap配置文章点击打开链接

 

调用系统的API 官方文档地址 点击打开链接

 

 

<1> 在控制台 创建一个phonegap工程 命令如下

  1. phonegap create my-app
  2. cd my-app
  3. phonegap run android
 phonegap create my-app
 cd my-app
 phonegap run android

 

 

 

<2> 将工程导入 eclipse

 

 

 

 

<3> 配置 cordova _plugins.js 文件

 

首先给大家看看cordova _plugins.js 文件:

 

  1. cordova.define('cordova/plugin_list', function(require, exports, module) {
  2. module.exports = [
  3. {
  4. "file": "plugins/org.apache.cordova.camera/www/CameraConstants.js",
  5. "id": "org.apache.cordova.camera.Camera",
  6. "clobbers": [
  7. "Camera"
  8. ]
  9. },
  10. {
  11. "file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
  12. "id": "org.apache.cordova.camera.CameraPopoverOptions",
  13. "clobbers": [
  14. "CameraPopoverOptions"
  15. ]
  16. },
  17. {
  18. "file": "plugins/org.apache.cordova.camera/www/Camera.js",
  19. "id": "org.apache.cordova.camera.camera",
  20. "clobbers": [
  21. "navigator.camera"
  22. ]
  23. },
  24. {
  25. "file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
  26. "id": "org.apache.cordova.camera.CameraPopoverHandle",
  27. "clobbers": [
  28. "CameraPopoverHandle"
  29. ]
  30. },
  31. {
  32. "file": "plugins/org.apache.cordova.dialogs/www/notification.js",
  33. "id": "org.apache.cordova.dialogs.notification",
  34. "merges": [
  35. "navigator.notification"
  36. ]
  37. },
  38. {
  39. "file": "plugins/org.apache.cordova.dialogs/www/android/notification.js",
  40. "id": "org.apache.cordova.dialogs.notification_android",
  41. "merges": [
  42. "navigator.notification"
  43. ]
  44. },
  45. {
  46. "file": "plugins/org.apache.cordova.vibration/www/vibration.js",
  47. "id": "org.apache.cordova.vibration.notification",
  48. "merges": [
  49. "navigator.notification"
  50. ]
  51. },
  52. {
  53. "file": "plugins/intent.js",
  54. "id": "org.apache.cordova.intent",
  55. "merges": [
  56. "navigator.intent"
  57. ]
  58. },
  59. ];
  60. module.exports.metadata =
  61. // TOP OF METADATA
  62. {
  63. "org.apache.cordova.camera": "0.2.7",
  64. "org.apache.cordova.dialogs": "0.2.6",
  65. "org.apache.cordova.vibration": "0.3.7",
  66. "org.apache.cordova.intent" :"0.0.1",
  67. }
  68. // BOTTOM OF METADATA
  69. });
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.camera/www/CameraConstants.js",
        "id": "org.apache.cordova.camera.Camera",
        "clobbers": [
            "Camera"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js",
        "id": "org.apache.cordova.camera.CameraPopoverOptions",
        "clobbers": [
            "CameraPopoverOptions"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.camera/www/Camera.js",
        "id": "org.apache.cordova.camera.camera",
        "clobbers": [
            "navigator.camera"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js",
        "id": "org.apache.cordova.camera.CameraPopoverHandle",
        "clobbers": [
            "CameraPopoverHandle"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.dialogs/www/notification.js",
        "id": "org.apache.cordova.dialogs.notification",
        "merges": [
            "navigator.notification"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.dialogs/www/android/notification.js",
        "id": "org.apache.cordova.dialogs.notification_android",
        "merges": [
            "navigator.notification"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.vibration/www/vibration.js",
        "id": "org.apache.cordova.vibration.notification",
        "merges": [
            "navigator.notification"
        ]
    },
    {
        "file": "plugins/intent.js",
        "id": "org.apache.cordova.intent",
        "merges": [
            "navigator.intent"
        ]
    },
];
module.exports.metadata = 
// TOP OF METADATA
{
    "org.apache.cordova.camera": "0.2.7",
    "org.apache.cordova.dialogs": "0.2.6",
    "org.apache.cordova.vibration": "0.3.7",
    "org.apache.cordova.intent" :"0.0.1",

}
// BOTTOM OF METADATA
});


我之前配置了camera ,dialog , vibration ,大家可以参考

现在来分解 ,这里要配置2个地方

module.exports= [{}];

module.exports.metadata = { }

 

在module.exports 的花括号里面配置

  1. {
  2. "file": "plugins/intent.js",
  3. "id": "org.apache.cordova.intent",
  4. "merges": [
  5. "navigator.intent"
  6. ]
  7. },
{
        "file": "plugins/intent.js",
        "id": "org.apache.cordova.intent",
        "merges": [
            "navigator.intent"
        ]
    },

 

file 代表 javascript写的接口位置

id 代表 唯一

merges 代表你在 javascript中调用该接口的语句 (类似activity中的 getApplication() 等等 ;就是个调用语句)

 

在module.exports.metadata 中配置id

标号随意

 

 

<4> 在plugin目录下编写javascript接口

 

贴上intent.js的接口代码

  1. cordova.define("org.apache.cordova.intent", function(require, exports, module) {
  2. var exec = require('cordova/exec');
  3. module.exports = {
  4. /**
  5. * 一共5个参数
  6. 第一个 :成功会掉
  7. 第二个 :失败回调
  8. 第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)
  9. 第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)
  10. 第五个 :传递的参数 以json的格式
  11. */
  12. demo: function(mills) {
  13. exec(function(winParam){
  14. alert(winParam);
  15. }, null, "Demo", "intent", [mills]);
  16. },
  17. };
  18. });
cordova.define("org.apache.cordova.intent", function(require, exports, module) { 

var exec = require('cordova/exec');



module.exports = {

    /**
     * 一共5个参数
       第一个 :成功会掉
       第二个 :失败回调
       第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)
       第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)
       第五个 :传递的参数  以json的格式
     */
    demo: function(mills) {
        exec(function(winParam){
        	alert(winParam);
        }, null, "Demo", "intent", [mills]);
    },
};

});


Demo中成功返回 会弹出一个Alert();

 

在javascript中的 调用语句是

  1. navigator.intent.demo(1);
  navigator.intent.demo(1);    

 

 

贴上整的javascript

 

 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Notification Example</title>
  5. <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
  6. <script type="text/javascript" charset="utf-8">
  7. // Wait for device API libraries to load
  8. //
  9. document.addEventListener("deviceready", onDeviceReady, false);
  10. // device APIs are available
  11. //
  12. function onDeviceReady() {
  13. // Empty
  14. }
  15. // Show a custom alert
  16. // native的 Dialog 对话框
  17. function showAlert() { navigator.notification.alert( 'You are the winner!', // message
  18. 'Game Over',
  19. // title
  20. 'Done'
  21. // buttonName
  22. ); }
  23. // Beep three times
  24. // 响铃3声
  25. function playBeep() { navigator.notification.beep(3); }
  26. // Vibrate for 2 seconds
  27. // 振动
  28. function vibrate() { navigator.notification.vibrate(100000); }
  29. // 跳转
  30. function intent() { navigator.intent.demo(1); }
  31. </script>
  32. </head>
  33. <body>
  34. <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>
  35. <p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>
  36. <p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>
  37. <p><a href="#" onclick="intent(); return false;">Html跳转到android界面</a></p>
  38. </body>
  39. </html>
<!DOCTYPE html>
<html>  
<head>   
  <title>Notification Example</title> 
  <script type="text/javascript" charset="utf-8" src="cordova.js"></script>    
  <script type="text/javascript" charset="utf-8">    
   // Wait for device API libraries to load    
   //    
   document.addEventListener("deviceready", onDeviceReady, false);    
   // device APIs are available    
   //    
   function onDeviceReady() {        
   	 // Empty    
  	}    
    // Show a custom alert    
    
    //    native的 Dialog 对话框
    function showAlert() {        navigator.notification.alert(            'You are the winner!',  // message            
      'Game Over',            
    // title            
    'Done'                  
    // buttonName        
    );    }    
    // Beep three times    
    //    响铃3声
    function playBeep() {        navigator.notification.beep(3);    }    
    // Vibrate for 2 seconds    
    //    振动
    function vibrate() {         navigator.notification.vibrate(100000);    }    
	//	     跳转
    function intent() {          navigator.intent.demo(1);    		}    

    </script>  
    </head>  
    <body>    
    <p><a href="#" onclick="showAlert(); return false;">Show Alert</a></p>    
    <p><a href="#" onclick="playBeep(); return false;">Play Beep</a></p>    
    <p><a href="#" onclick="vibrate(); return false;">Vibrate</a></p>  
    <p><a href="#" onclick="intent(); return false;">Html跳转到android界面</a></p>  
    </body>
    </html>


 

 

 

 

 

<5> 在res/xml 目录下配置 config.xml 文件

 

 

config.xml配置 加上 如下

  1. <feature name="Demo">
  2. <param name="android-package" value="plugins.Plugin_intent" />
  3. </feature>
    <feature name="Demo">
        <param name="android-package" value="plugins.Plugin_intent" />
    </feature>


feature的name属性 非常重要

name必须是步骤< 4 >中 function中调用的类名

value属性指定插件在src目录下的java文件 (命名空间)

 

 

今天就写到这里

 

 

example:

明天整理后提交 今晚先睡了 ~~~谢谢

插件Demo下载地址: http://download.csdn.net/detail/aaawqqq/6992627

 

 

 

 

工程下载 将phonegap的platforms导入到eclipse中

如果报错clear一下 查看导的lib包 有没有报错

如果还有错 那么就是您选用了 google的API 改成最新版的android API 就好了

具体排查错误 可以看我的这一篇Blog: http://blog.csdn.net/aaawqqq/article/details/20463183

posted @ 2014-12-20 16:44  net5x  阅读(222)  评论(0)    收藏  举报