native.js虽然强大和开放,但很多web开发者因为不熟悉原生api而难以独立完成。
这篇帖子的目的就是汇总各种写好的njs代码,方便web开发者。
众人拾柴火焰高,有能力的开发者多多提交njs代码,大家都会给你点赞的,
android平台
在桌面创建和删除app快捷方式
见hello h5+里native.js部分演示及源码。
或在这里搜索“快捷方式”,http://ask.dcloud.net.cn/docs/ ... le/88调用android本地分享
获取安卓设备device.uuid
数据读取
打开网络设置
调用系统控件播放视频
调用讯飞的文字转语音功能(tts)
接收系统广播消息,如监听安装卸载apk的事件
例说明如何使用native.js进行broadcastreceiver广播http://ask.dcloud.net.cn/question/7661常驻android通知栏,不用个推实现本地消息推送(local notification)
开启个推推送功能
var pushmanager = plus.android.importclass("com.igexin.sdk.pushmanager");
var context = plus.android.runtimemainactivity();
function enable() {
pushmanager.getinstance().turnonpush(context);
}
function disable() {
pushmanager.getinstance().turnoffpush(context);
}
感谢yeahcheung分享
利用native.js获取手机gps是否开启http://ask.dcloud.net.cn/question/11890通过native.js设置系统墙纸http://ask.dcloud.net.cn/article/651ios平台
获取包名
var nsbundle = plus.ios.importclass('nsbundle');
var bundle = nsbundle.mainbundle();
console.log(bundle.bundleidentifier());
plus.ios.deleteobject(bundle);
获取设备名
native.js登入game center
见hello h5+里native.js部分演示及源码。
或在这里搜索“game center”,http://ask.dcloud.net.cn/docs/ ... le/88设置获取内容到系统粘贴板
base64数据保存为图片http://ask.dcloud.net.cn/question/6190设置webview滑动减速度
var webview = plus.ios.currentwebview();
var scrollview = webview.plusgetattribute("scrollview");
scrollview.plussetattribute("decelerationrate:",0.99);
打开ios的wifi设置页面
http://ask.dcloud.net.cn/article/188ios获取系统的时区id
var nstimezone = plus.ios.importclass("nstimezone");
var sys = nstimezone.systemtimezone();
console.log(sys.plusgetattribute("name"));
状态栏显示网络请求雪花
var uiapplication = plus.ios.import("uiapplication");
var sharedapplication = uiapplication.sharedapplication();
sharedapplication.setnetworkactivityindicatorvisible(true);
plus.ios.deleteobject(sharedapplication);
获取gps授权状态
var cllocationmanager = plus.ios.import("cllocationmanager");
var authorizationstatus = cllocationmanager.authorizationstatus();
switch(authorizationstatus) {
case 0:
/// user has not yet made a choice with regards to this application
break;
case 1:
// this application is not authorized to use location services. due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization
break;
case 2:
// user has explicitly denied authorization for this application, or
// location services are disabled in settings.
break;
case 3:
// user has granted authorization to use their location at any time,
// including monitoring for regions, visits, or significant location changes.
break;
case 4:
// user has granted authorization to use their location only when your app
// is visible to them (it will be made visible to them if you continue to
// receive location updates while in the background). authorization to use
// launch apis has not been granted.
break;
case 5:
// this value is deprecated, but was equivalent to the new -always value.
break;
defalut:
break;
}
获取手机存储空间
var bundleclass = plus.ios.importclass("nsbundle");
var bundleobj = bundleclass.mainbundle();
var filenamagerobj = plus.ios.newobject("nsfilemanager");
var fileattr = plus.ios.invoke(filenamagerobj,"attributesoffilesystemforpath:error:",bundleobj.bundlepath(),null);
// nsfilesystemfreesize 参数获取剩余空间
// nsfilesystemsize 获取手机总存储空间
var freespace = plus.ios.invoke(fileattr,"objectforkey:","nsfilesystemfreesize");
var numberformatterobj = plus.ios.newobject("nsnumberformatter");
var freespacestr = plus.ios.invoke(numberformatterobj,"stringfromnumber:",freespace);
var freespace = freespacestr / 1024/1024/1024;
看完这些例子,是不是觉得js特别强大?
赶快拿去用起来吧!
以上就是javascript强化教程――native.js示例汇总的内容。