轻应用开发 API

app

基础功能接口,使用该模块必须先引入Cordova.js提供网络请求、页面切换、数据存储、文件操作、设备操作等通用接口。
更多Cordova接口

接口模块分类

barcode

broadcast

database

dateTimePicker

link

notification

page

pageHeader

phone

progress

recode

setting

timetask

todayStepCount

utils

wheelSelect

wxPay

Methods

static ajax(params)

发送网络请求

Parameters:
Name Type Description
params Object

url:网络请求地址
data:请求的参数,JSON对象
method:请求的方法,post|get,默认是get
async:true|false(默认)
contentType:将数据发到服务器时浏览器使用的编码类型,默认值是"application/x-www-form-urlencoded"
headers: http请求头,JSON对象
timeout: 超时时间,单位ms,默认10000毫秒
success:请求成功时的回调函数
fail:请求失败时的回调函数

Example
app.ajax({
    "url":"http://10.201.76.142:8500/dataservice.ashx",
    "data":{type:'news'},
    "timeout" :5000,
    "success" : function(res){
        var data = res.returnValue;
        app.alert(data);
    }
}) ;
//return
{ name:'yulsh' , sex:'男', age: '25' }

static ajaxWSDL(params)

发送网络请求,请求wsdl(webservice)

Parameters:
Name Type Description
params Object

method:调用的方法名
data:方法参数,json对象
namespace:wsdl的命名空间
endpoint:wsdl的请求地址
timeout:请求超时时间,默认是10000 毫秒
success:成功回调,返回数据(字符串)
fail:失败回调,返回错误信息(字符串)

Example
var params={
  method:"getStoreInfo",   //调用的方法
  data:{"storeId":123},   //方法参数,json对象
  namespace:"http://webservice.gmcc.com/",   //wsdl的命名空间
  endpoint:"http://183.232.148.39:7655/fdaims/webservice/FdaimsWebservices",   //wsdl的地址(去掉?wsdl)
  success:function(res){alert(res);},   //成功回调
  fail:function(error){alert(error);}   //失败回调
};
app.ajaxWSDL(params);

static alert(message, callback, title, buttonName)

弹出提示框

Parameters:
Name Type Description
message String

窗口消息内容

callback function

回调函数

title String

窗口标题

buttonName String

按钮名称

Example
app.alert("这是一个定制的提示框", function(){
    app.hint("我有一个回调事件");
}, "温馨提示", "OK");

static back(callback)

返回上个页面,返回的时候可以在上个页面执行相关逻辑,适用于多页面

Parameters:
Name Type Description
callback function

回调函数,可以是一个方法签名,也可以是匿名函数

Example
//传入方法签名
app.back("test('abc')"); //需要在上个页面声明 test(a)方法
app.back(function(){ $(".span>h1").text("BingoTouch");}); //执行匿名函数

static confirm(message, callback, title, buttonNames)

弹出确认框

Parameters:
Name Type Description
message String

窗口消息内容

callback function

回调函数

title String

窗口标题

buttonNames String

按钮组的名称,例:"确定,取消"

Example
app.confirm("确定要使用BingoTouch吗?", function(index){
    if(index==1){
        app.hint("我点击了OK");
    }else{
        app.hint("我点击了Cancel");
    }
}, "请您确认", "OK,Cancel");

static exit()

退出程序(android支持退出app,ios下通常不退出,在iOS尽量不要用此接口,在Link中可以通过此接口回到Link应用

Example
app.exit();

static get(url, data, success, fail)

发送GET请求

Parameters:
Name Type Description
url String

请求地址

data Object

请求参数

success function

成功调用函数

fail function

失败调用函数

Example
var url="http://10.201.76.142:8500/dataservice.ashx?type=date";
app.get(url,{},function(res){
    $("#result").html("返回值类型:"+typeof(res)+" 结果:"+ JSON.stringify(res));
},function(res){
    app.alert(res);
});
//return
{"code":"200","returnValue":"2013/9/5 14:14:51"}

static getAppDirectoryEntry(callback)

获取app安装后的相关目录
android和ios的目录结构不同
android下可以存储在 /sdcard/download下面
ios只能存储在应用里面

Parameters:
Name Type Description
callback function

回调函数

Example
app.getAppDirectoryEntry(function(res){
     //区分平台,并将相应的目录保存到全局,方便下面下载的时候使用
     if(window.devicePlatform=="android"){
         window.savePath=res.sdcard;
     }else if(window.devicePlatform=="iOS"){
         window.savePath=res.documents;
     }
 });

static getCurrentUri(callback)

获取当前页面的地址

Parameters:
Name Type Description
callback function

回调函数

Example
app.getCurrentUri(function(res){
    $("#result").html("返回值类型:"+typeof(res)+" 结果:"+ JSON.stringify(res));
});

static getGlobalVariable(key, callback)

获取运行时全局变量

Parameters:
Name Type Description
key String

callback function

回调函数

Example
app.getGlobalVariable("name",function(res){
      app.alert("返回值类型:"+typeof(res)+" 结果:"+ JSON.stringify(res));
  });

static getInfo(callback)

获取app相关信息
res包含三个属性,id:程序的id号、versionCode:版本编码、versionName:版本名称

Parameters:
Name Type Description
callback function

回调函数

Example
app.getInfo(function(res){
     app.alert(res);
 });

static getLocation(success, fail, params)

获取当前坐标

Parameters:
Name Type Description
success function

成功回调函数,返回json对象

fail function

失败回调函数,返回错误信息

params Object

拓展参数对象
needBackGroundLocation: 是否后台持续定位。1表示需要后台持续定位 0表示不需要后台持续定位

static getPageParams(callback)

获取页面传递的参数

Parameters:
Name Type Description
callback function

回调函数,注意这个回调函数是有返回结果的

Example
app.getPageParams(function(result){
    var name = result.name;
});

static getSize(callback)

获取设备的尺寸
height:屏幕的高度、width:屏幕的宽度

Parameters:
Name Type Description
callback function

回调函数

Example
app.getSize(function(res){
      app.alert(res);
  });

static hint(message, pasition)

显示提示信息

Parameters:
Name Type Description
message String

提示信息

pasition String

提示语位置 top|bottom(default)

Example
app.hint("Hello,World");

static install(fileUrl, success, fail)

安装应用(仅android平台适用,iOS平台是通过请求plist的方式安装

Parameters:
Name Type Description
fileUrl String

文件路径

success function

安装成功回调

fail function

安装失败回调

Example
app.install(fileUrl);

static isExist(appId, callback)

检测是否存在某个app
android:传入包名 eg: bingo.touch.debugger
ios:urlSchemes eg: bingo-debugger://

Parameters:
Name Type Description
appId String

应用Id

callback function

回调函数

Example
app.isExist("bingo.touch.debugger",function(res){
      if(res){
          app.hint("存在appId为bingo.touch.debugger的应用!");
      }else{
          app.hint("不存在appId为bingo.touch.debugger应用!")
      }
  });

static load(params)

加载页面

Parameters:
Name Type Description
params Object

请求参数,详情如下
url:切换页面的url
params:页面传递的参数,一个JSON对象
slideType:页面切换的方向left|right|up|down

Example
app.load({
    url:"http://www.baidu.com",
    params:{name:"yulsh", sex:"男"},
    slideType:"left"
});

static loadWithUrl(url, params, slideType)

加载页面

Parameters:
Name Type Description
url String

切换页面的url

params Object

页面传递的参数

slideType String

页面切换的方向

Example
app.loadWithUrl('modules/test/secondpage.html',{},'left');

static openContactSelector(callback)

打开本地通讯录选择通讯录信息

Parameters:
Name Type Description
callback function

回调函数,返回json数组,包含名称和手机号等

static openFile(filePath, mime, success, fail)

打开文件:如office文件,apk等,将调用系统的程序来打开文件

Parameters:
Name Type Description
filePath String

文件地址

mime String

mime类型

success function

打开成功回调

fail function

打开失败回调

Example
app.openFile("file:///mnt/sdcard/bingotouch.docx","docx",function(res){
     app.hint("打开成功!");
 });

static post(url, data, success, fail)

发送POST请求

Parameters:
Name Type Description
url String

请求地址

data Object

JSON对象,请求参数

success function

成功调用函数

fail function

失败调用函数

Example
var url="http://10.201.76.142:8500/dataservice.ashx";
 app.post(url,{type:"date"},function(res){
     $("#result").html("返回值类型:"+typeof(res)+" 结果:"+ JSON.stringify(res));
 },function(res){
     app.alert(res);
 });
 //return
 {"code":"200","returnValue":"2013/9/5 14:14:51"}

static refresh()

刷新当前页面

Example
app.refresh();

static rotation(type)

控制屏幕旋转

Parameters:
Name Type Description
type string

landscape 横屏锁定、portrait 竖屏锁定

Example
app.rotation("landscape"); //表示当前页面需要横屏锁定显示;
        app.rotation("portrait"); //表示竖屏锁定
        app.rotation("user");  //解除锁定,恢复横竖屏(仅android)。

static run(appId, data)

执行第三方的应用,如果是传入http的远程地址,将会调用系统自带的浏览器打开远程页面
android: package name eg: bingo.touch.debugger
ios: urlSchemes eg: http://www.baidu.com open safari

Parameters:
Name Type Description
appId String

应用Id

data Object

启动参数

Example
app.run("bingo.touch.demo",{
     "user" : "yulsh",
     "status" : 1
 });

static setGlobalVariable(key, value)

设置运行时全局变量

Parameters:
Name Type Description
key String

value String

Example
app.setGlobalVariable("name","yulsh");

static setResult(data)

获取页面传递的参数

Parameters:
Name Type Description
data Array

传递参数

Example
app.setResult([1,2,3]);

static vibrate(mills)

设备震动提醒

Parameters:
Name Type Description
mills int

毫秒ms