腊月的季节

cordova平台总结一

Splashscreen插件

显示或隐藏启动屏幕画面

1
2
3
4
5
6
module.controller('MyCtrl', function($scope, $cordovaSplashscreen) {

$cordovaSplashscreen.show();
//$cordovaSplashscreen.hide();

});

StatusBar插件

配置设备的状态栏颜色和样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module.controller('MyCtrl', function($cordovaStatusbar) {
$cordovaStatusbar.overlaysWebView(true);

// 样式: 无 : 0, 白色不透明: 1, 黑色半透明: 2, 黑色不透明: 3
$cordovaStatusbar.style(1);

// 背景颜色名字 : black, darkGray, lightGray, white, gray, red, green,
// blue, cyan, yellow, magenta, orange, purple, brown 注:需要开启状态栏占用视图.
$cordovaStatusbar.styleColor('black');

$cordovaStatusbar.styleHex('#000');

$cordovaStatusbar.hide();

$cordovaStatusbar.show();

var isVisible = $cordovaStatusbar.isVisible();

});

Badge

修改应用程序图标的角标数量,Android、ios和Windows手机。
方法
hasPermission()
检测是否有修改权限

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.hasPermission().then(function(yes) {
// 有权限
}, function(no) {
// 无权限
});

});

set(badge, callback, scope)
设置角标数字

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.set(3).then(function() {
// 有权限, 已设置.
}, function(err) {
// 无权限
});

});

get()
获取角标数字

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.get().then(function(badge) {
// 有权限, 已返回.
}, function(err) {
// 无权限
});

});

clear(callback, scope)
清除角标数字

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.clear().then(function() {
// 有权限, 已清除.
}, function(err) {
// 无权限
});

});

increase(count, callback, scope)
增加角标数字,如果不设置则+1

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.increase(10).then(function() {
// 有权限, 已增加10.
}, function(err) {
// 无权限
});

});

decrease(count, callback, scope)
减少角标数字,如果不设置则-1

1
2
3
4
5
6
7
8
9
module.controller('MyCtrl', function($cordovaBadge) {

$cordovaBadge.decrease().then(function(badge) {
// 有权限, 已减少10.
}, function(err) {
// 无权限
});

});

network插件

方法
getNetwork()
提供了一个快速的方法来确定设备的网络连接状态,和类型的连接。

返回值 Connection Object:

Connection Type(连接类型) 说明
Connection.UNKNOWN 未知连接
Connection.ETHERNET 以太网连接
Connection.WIFI 无线网连接
Connection.CELL_2G 2G网连接
Connection.CELL_3G 3G网连接
Connection.CELL_4G 4G网连接
Connection.CELL 通用连接
Connection.NONE 无网络连接

isOnline()
检测手机网络是否在线

返回值 如果为true手机网络在线
isOffline()
检测手机网络是否离线

返回值 如果为true手机网络离线
事件(Event)
$cordovaNetwork:online
监听$cordovaNetwork:online当手机上网时触发事件
返回值

参数 类型 说明
event Object Angular 事件 $broadcast - 详情查阅 AngularJS docs
networkState Object 网络连接类型,具体参考getNetwork()的返回值

$cordovaNetwork:offline
监听$cordovaNetwork:offline当手机网络离线时触发事件

参数 类型 说明
event Object Angular 事件 $broadcast - 详情查阅 AngularJS docs
networkState Object 网络连接类型,具体参考getNetwork()的返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
document.addEventListener("deviceready", function () {
// 监听手机网络在线事件
$rootScope.$on('$cordovaNetwork:online', function(event, networkState){
var onlineState = networkState;
$rootScope.netState=1;
});

// 监听手机网络离线事件
$rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
$cordovaToast.showLongBottom('亲!此应用需要联网\\(^o^)/');
$rootScope.netState=0;
});

}, false);

Toast插件

这个插件可以显示一个像原生Toast(显示一小段文字提示)
方法
show(message, duration, position)
增加一个或多个本地通知

参数 类型 说明
message String 信息提示的一段文字
duration String 信息提示显示持续事件 长或短: ‘short’, ‘long’
position String 信息提示显示的位置顶部,中间,底部: ‘top’, ‘center’,’bottom’

如果不想填写额外参数可以通过更优雅的方式显示 Toast(提示信息)
showShortTop(message) 显示顶部

showShortCenter(message) 显示中间

showShortBottom(message) 显示底部

showLongTop(message) 显示顶部(长时间)

showLongCenter(message)显示中间(长时间)

showLongBottom(message)显示底部(长时间)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module.controller('MyCtrl', function($cordovaToast) {

$cordovaToast
.show('ionic中文网', 'long', 'center')
.then(function(success) {
// success
}, function (error) {
// error
});

$cordovaToast.showShortTop('ionic中文网').then(function(success) {
// success
}, function (error) {
// error
});

$cordovaToast.showLongBottom('ionic中文网').then(function(success) {
// success
}, function (error) {
// error
});

})

Dialogs插件

显示一个系统提示对话框
方法
alert(message, title, buttonName)

参数 类型 说明
message String 对话框提示的一段文字
title String 对话框标题 默认:alert
buttonName String 对话框按钮名称 默认:ok

confirm(message, title, buttonArray)
显示一个带有指定消息和取消及取消按钮的对话框(可以自定义两个按钮的名称)

参数 类型 说明
message String 对话框提示的一段文字
title String 对话框标题 默认:alert
buttonArray Array 按钮名名称 是一个数组 默认:ok,cancel

返回值 Integer 1或2取决于你点击了哪一个按钮
prompt(message, title, buttonArray, defaultText)
显示可提示用户进行输入的对话框

参数 类型 说明
message String 对话框提示的一段文字
title String 对话框标题 默认:alert
buttonArray Array 按钮名名称 是一个数组 默认:ok,cancel
defaultText String 用户输入提示信息

返回值 Object 用于接受用户输入:result.input1 判断用户点击那哪一个按钮 返回一个索引:result.buttonIndex

beep(repetitions)
显示可提示用户进行输入的对话框

参数 类型 说明
repetitions Integer 设置弹出对话框延迟以秒为单位
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module.controller('MyCtrl', function($scope, $cordovaDialogs) {

$cordovaDialogs.alert('message', 'title', 'button name')
.then(function() {
// callback success
});

$cordovaDialogs.confirm('message', 'title', ['button 1','button 2'])
.then(function(buttonIndex) {
// no button = 0, 'OK' = 1, 'Cancel' = 2
var btnIndex = buttonIndex;
});

$cordovaDialogs.prompt('msg', 'title', ['btn 1','btn 2'], 'default text')
.then(function(result) {
var input = result.input1;
// no button = 0, 'OK' = 1, 'Cancel' = 2
var btnIndex = result.buttonIndex;
});

// beep 3 times
$cordovaDialogs.beep(3);

});

Pin Dialog

数字密码对话框。
方法
prompt(message)

显示一个可以输入数字密码的对话框
|参数|类型|说明|
|-|-|-|
|message|String|信息提示的一段文字|

1
2
3
4
5
6
7
8
9
10
module.controller('MyCtrl', function($cordovaPinDialog) {

$cordovaPinDialog.prompt('Some message here').then(
function(result) {
// result
},
function (error) {
// error
})
});

action Sheet插件

显示一个用户可以选择的原生上拉菜单.iOS调用UIActionSheet. Android调用AlertDialog。
方法
show(options)

参数 类型 说明
options Object Options的选项
Options 类型 说明
title String 标题
buttonLabels String Array 每个按钮的文本,从1开始
addCancelButtonWithLabel String 如果为空,没有取消按钮。否则,设置取消按钮的文字
androidEnableCancelButton Boolean 显示Android取消按钮,默认为假
winphoneEnableCancelButton Boolean 显示IOS取消按钮,默认为假
addDestructiveButtonWithLabel String 添加一个红色的按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module.controller('ThisCtrl', function($cordovaActionSheet) {

var options = {
title: '你要做什么?',
buttonLabels: ['分享到新浪微博', '分享到朋友圈'],
addCancelButtonWithLabel: '取消',
androidEnableCancelButton: true,
winphoneEnableCancelButton: true,
addDestructiveButtonWithLabel: '删除这个'
};

document.addEventListener("deviceready", function () {

$cordovaActionSheet.show(options)
.then(function(btnIndex) {
var index = btnIndex;
});
}, false);

});

Date Picker

显示本地日期和时间选择器控件
方法
show(options)
显示一个日期选择器
options是一个时间选择器的选项。它是一个Object

参数 类型 默认值 说明
options.mode String ‘date’ 日期选择器模式:date 或者time
options.date Date, String new Date() 选择日期
options.minDate Date, String 最小日期
options.maxDate Date, String 最大日期
options.allowOldDates Boolean true 显示/隐藏早些时候的日期
options.allowFutureDates Boolean true 显示/隐藏以后的的日期
options.doneButtonLabel String Done’ 完成按钮名称
options.doneButtonColor String ‘#0000FF’ 完成按钮颜色16进制代码
options.cancelButtonLabel String ‘Cancel’ 取消按钮名称
options.cancelButtonColor String ‘#000000’ 取消按钮颜色16进制代码
options.minuteInterval Integer 1 选择器间隔时间

返回值 Date 用户选择的时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module.controller('MyCtrl', function($scope, $cordovaDatePicker) {

var options = {
date: new Date(),
mode: 'date', // or 'time'
minDate: new Date() - 10000,
allowOldDates: true,
allowFutureDates: false,
doneButtonLabel: 'DONE',
doneButtonColor: '#F2F3F4',
cancelButtonLabel: 'CANCEL',
cancelButtonColor: '#000000'
};

document.addEventListener("deviceready", function () {

$cordovaDatePicker.show(options).then(function(date){
alert(date);
});

}, false);
});

native audio

Cordova / PhoneGap 3.5 +本地音频播放,针对HTML5游戏和音频应用所需的最小延迟,复调和并发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module.controller('MyCtrl', function($scope, $cordovaNativeAudio, $timeout) {

$cordovaNativeAudio
.preloadSimple('click', 'audio/click.mp3')
.then(function (msg) {
console.log(msg);
}, function (error) {
alert(error);
});

$cordovaNativeAudio
.preloadComplex('music', 'audio/music.mp3', 1, 1)
.then(function (msg) {
console.log(msg);
}, function (error) {
console.error(error);
});

$scope.play = function () {
$cordovaNativeAudio.play('click');
$cordovaNativeAudio.loop('music');

// stop 'music' loop and unload
$timeout(function () {
$cordovaNativeAudio.stop('music');

$cordovaNativeAudio.unload('click');
$cordovaNativeAudio.unload('music');
}, 1000 * 60);
};

});

Barcode Scanner

打开相机自动扫描 条形码/二维码,返回数据。
方法
scan()
返回 对象 - 信息
encode(type, text)
|参数|类型|说明|
|-|-|-|
|type|Constant|编码类型(如:barcodescanner。编码。text_type)|
|text|String|编码所需的文本字符串|

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module.controller('BarcodeCtrl', function($scope, $cordovaBarcodeScanner) {

document.addEventListener("deviceready", function () {

$cordovaBarcodeScanner
.scan()
.then(function(barcodeData) {
// Success! Barcode data is here 扫描数据:barcodeData.text
}, function(error) {
// An error occurred
});


// NOTE: encoding not functioning yet
$cordovaBarcodeScanner
.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com")
.then(function(success) {
// Success!
}, function(error) {
// An error occurred
});

}, false);
});

Vibration插件

使设备震动

1
2
3
4
5
6
module.controller('MyCtrl', function($scope, $cordovaVibration) {

// 震动 100ms
$cordovaVibration.vibrate(100);

});

SQLite插件

调用SQLite本地数据库储存接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module.controller('MyCtrl', function($scope, $cordovaSQLite) {

var db = $cordovaSQLite.openDB({ name: "my.db" });

// for opening a background db:
var db = $cordovaSQLite.openDB({ name: "my.db", bgType: 1 });

$scope.execute = function() {
var query = "INSERT INTO test_table (data, data_num) VALUES (?,?)";
$cordovaSQLite.execute(db, query, ["test", 100]).then(function(res) {
console.log("insertId: " + res.insertId);
}, function (err) {
console.error(err);
});
};

});

热评文章