这篇主要给大家介绍一些phonegap中调用本地API的插件

一、都有哪些插件:

Accelerometer 设备点击屏幕手势感应器(有了这个就可以开发重力感应类的游戏了)

Camrea 调用设备摄像头照相采集照片 

Capture 使用设备的媒体采集应用程序调用媒体文件

Compass 获取设备移动的方向

Connection 快速检查网络状况以及蜂窝网络的信息

Contacts 设备联系人相关操作

Device 获取设备信息

Events 通过JavaScript获取本地活动

File 通过JavaScript调用本地文件系统

Geolocation 使应用程序可以访问地理位置信息

Globalization 国际化

Media 录制和播放音频文件

Notification 设备视觉、声音和触觉反馈

Splashscreen 启动画面

Storage 截获设备的本地存储选项

 

二、插件的使用:

首先是要在调用插件的html中引入cordova-2.2.0.js(在下载的资源中lib\android目录下);

接下来我们来看两种比较常用的插件的使用:

1、File 本地文件的读写

文件的写入

   
   
   
   
  1. // Wait for Cordova to load
  2. document.addEventListener("deviceready", onDeviceReady, false); 
  3.  
  4. // Cordova is ready 
  5. function onDeviceReady() { 
  6.     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
  7.  
  8. function gotFS(fileSystem) { 
  9. // 这里的文件路径为相对路径,根目录为file:///mnt/sdcard 
  10.     fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail); 
  11.  
  12. function gotFileEntry(fileEntry) { 
  13.     fileEntry.createWriter(gotFileWriter, fail); 
  14.     //fileEntry.createWriter(seek_ex, fail); 
  15.  
  16. //seek是指从原文件第几个字符开始写入
  17. function seek_ex(writer) { 
  18.     writer.onwrite = function(evt) { 
  19.         console.log("write success"); 
  20.     };  
  21.     writer.seek(writer.length); 
  22.     writer.write("appended text"); 
  23. }; 
  24.  
  25. function gotFileWriter(writer) { 
  26.     writer.onwriteend = function(evt) { 
  27.         console.log("contents of file now 'some sample text'"); 
  28.         writer.truncate(21);  //存21个字节
  29.         writer.onwriteend = function(evt) { 
  30.             console.log("contents of file now 'some sample'"); 
  31.         }; 
  32.     }; 
  33.     writer.write("some sample text读文件");//写入
  34.  
  35. function fail(error) { 
  36.     console.log(error.code); 

文件的读出,这里我们要注意的是,我们读取的文件最好为UTF-8编码格式的,否则中文会乱码

   
   
   
   
  1. // Wait for Cordova to load 
  2.     function onLoad() { 
  3.         document.addEventListener("deviceready", onDeviceReady, false); 
  4.     } 
  5.  
  6.     // Cordova is ready 
  7.     function onDeviceReady() { 
  8.         window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
  9.     } 
  10.  
  11.     function gotFS(fileSystem) { 
  12.         fileSystem.root.getFile("readme.txt"null, gotFileEntry, fail); 
  13.     } 
  14.  
  15.     function gotFileEntry(fileEntry) { 
  16.         fileEntry.file(gotFile, fail); 
  17.     } 
  18.  
  19.     function gotFile(file){ 
  20.         readAsText(file); 
  21.     } 
  22.  
  23.     function readAsText(file) { 
  24.         var reader = new FileReader(); 
  25.         reader.onloadend = function(evt) { 
  26.             console.log("Read as text"); 
  27.             $("#contents").append("
    Read as text
    "
    ); 
  28.             console.log(evt.target.result); 
  29.             $("#contents").append("
    "+evt.target.result+"
    "
    ); 
  30.         }; 
  31.         reader.readAsText(file); 
  32.     } 
  33.  
  34.     function fail(evt) { 
  35.         console.log(evt.target.error.code); 
  36.     } 

2、Storage的数据存储

phonegap的数据存储提供了两种方式,一种是localStorage,还有一种是sqlite数据库。

localStorage是html5所自带的一种数据存储方式,即使没有phonegap也是可以用的,

我们主要介绍sqlite数据库的增删查改调用。

顺带给一段localStorage的简单使用示例:(其实window.localStorage就是一个类似于json结构的对象)

   
   
   
   
  1. window.localStorage.setItem("key""value"); 
  2. var keyname = window.localStorage.key(i); 
  3. // keyname is now equal to "key" 
  4. var value = window.localStorage.getItem("key"); 
  5. // value is now equal to "value" 
  6. window.localStorage.removeItem("key"); 
  7. window.localStorage.setItem("key2""value2"); 
  8. window.localStorage.clear(); 

下面来看sqlite的调用:

   
   
   
   
  1. // Wait for Cordova to load 
  2.     // 
  3.     document.addEventListener("deviceready", onDeviceReady, false); 
  4.  
  5.     // Populate the database  
  6.     // 建表,插入字段
  7.     function populateDB(tx) { 
  8.         tx.executeSql('DROP TABLE IF EXISTS DEMO'); 
  9.         tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)'); 
  10.         tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")'); 
  11.         tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")'); 
  12.     } 
  13.  
  14.     // Query the database 
  15.     // 插入成功后调用,查询表内现有所有字段
  16.     function queryDB(tx) { 
  17.         tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB); 
  18.     } 
  19.  
  20.     // Query the success callback 
  21.     // 查询成功后调用
  22.     function querySuccess(tx, results) { 
  23.         var len = results.rows.length; 
  24.         console.log("DEMO table: " + len + " rows found."); 
  25.         for (var i=0; i
  26.             console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data); 
  27.             $('#database').append(""+results.rows.item(i).id+""+results.rows.item(i).data+""); 
  28.         } 
  29.     } 
  30.  
  31.     // Transaction error callback 
  32.     // 
  33.     function errorCB(err) { 
  34.         console.log("Error processing SQL: "+err.code); 
  35.     } 
  36.  
  37.     // Transaction success callback 
  38.     // 
  39.     function successCB() { 
  40.         var db = window.openDatabase("Database""1.0""Cordova Demo", 200000); 
  41.         db.transaction(queryDB, errorCB); 
  42.     } 
  43.  
  44.     // Cordova is ready 
  45.     // 
  46.     function onDeviceReady() { 
  47.         var db = window.openDatabase("Database""1.0""Cordova Demo", 200000); 
  48.         db.transaction(populateDB, errorCB, successCB); 
  49.     } 

以上代码介绍了插入与查询,修改与删除的方法类似于插入,只是sql语句不同,请自行补充sqlite的sql语句。

 

下集预告:phonegap的插件开发

欲知后事如何,请听下回分解!