Native.js⽰例汇总
Native.js虽然强⼤和开放,但很多web开发者因为不熟悉原⽣API⽽难以独⽴完成。
这篇帖⼦的⽬的就是汇总各种写好的NJS代码,⽅便web开发者。
众⼈拾柴⽕焰⾼,有能⼒的开发者多多提交NJS代码,⼤家都会给你点赞的,我们也会为每位共享NJS代码的朋友送上200积分。Android平台
Android动态权限申请
监听⼿机锁屏,解锁,开屏
直接
将程序切换到后台
强制弹出软键盘
获取安卓设备device.uuid
获取内存及CPU信息
开启关闭蓝⽛
监听蓝⽛开关状态
获取蓝⽛设备列表
蓝⽛连接票据打印机完整代码
NFC数据读取
截屏
获取MAC地址
获取设备当前⽹速
打开⽹络设置
打开各种系统设置界⾯
获取WIFI列表
调⽤系统控件播放视频
调⽤os通讯录选择控件
原⽣⽇历提醒插⼊
调⽤系统控件裁剪图⽚
复制内容到系统粘贴板
调⽤讯飞的⽂字转语⾳功能(TTS)
调⽤其它Activity后通过startActivityForResult获取返回结果
接收系统⼴播消息,如监听安装卸载apk的事件
判断app是否安装
以监听⼿机飞⾏模式开关为例说明如何使⽤Native.js进⾏BroadcastReceiver⼴播
常驻Android通知栏,不⽤个推实现本地消息推送(Local Notification)
调⽤原⽣的socket连接
启动⼀个原⽣service
基于native.js的⽂件系统管理功能实现
打开闪光灯
停⽌、开启个推推送功能
var pushManager = plus.android.importClass("com.igexin.sdk.PushManager");  var context = plus.android.runtimeMainActivity();
function enable() {
}
function disable() {
}
感谢分享
利⽤native.js获取⼿机gps是否开启
通过native.js设置系统墙纸
监听短信验证码
限制⼿机录像时间
Android监听电话呼⼊呼出
Android6以上动态权限申请
通知栏下载进度条
打开应⽤市场
iOS平台
如何判断系统功能权限是否开启及提⽰⽤户开启权限获取包名
var NSBundle = plus.ios.importClass('NSBundle');
var bundle = NSBundle.mainBundle();
console.log(bundle.bundleIdentifier());
plus.ios.deleteObject(bundle);
获取设备名
测试是否安装某应⽤
调⽤iOS打印API
通过native.js登⼊game center
设置获取内容到系统粘贴板
打开页⾯默认弹出键盘
播放提⽰⾳
调⽤ios的⽂字转语⾳(TTS)
把base64数据保存为图⽚
设置webview滑动减速度
var webview = plus.ios.currentWebview();
var scrollView = webview.plusGetAttribute("scrollView");
scrollView.plusSetAttribute("decelerationRate:",0.99);
打开ios的Wifi设置页⾯
判断是否开启消息通知
检测iOS是否允许使⽤相机(感谢分享)
打开应⽤市场
ios获取系统的时区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.
原生安卓app开发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 = wObject("NSFileManager");
var FileAttr = plus.ios.invoke(filenamagerobj,"attributesOfFileSystemForPath:error:",BundleObj.bundlePath(),null);  // NSFileSystemFreeSize 参数获取剩余空间
// NSFileSystemSize  获取⼿机总存储空间
var freeSpace = plus.ios.invoke(FileAttr,"objectForKey:","NSFileSystemFreeSize");
var numberFormatterObj = wObject("NSNumberFormatter");
var FreeSpaceStr = plus.ios.invoke(numberFormatterObj,"stringFromNumber:",freeSpace);
var freeSpace = FreeSpaceStr / 1024/1024/1024;
打开/关闭⼿机的闪光灯
function turnonLight(isOn) {
if(plus.os.name == "iOS") {
var avcaptClass = plus.ios.importClass("AVCaptureDevice");
if(avcaptClass) {
var device = avcaptClass.defaultDeviceWithMediaType("vide");
plus.ios.invoke(device, "lockForConfiguration:", null);
if(isOn) {
plus.ios.invoke(device, "setTorchMode:", 1);
plus.ios.invoke(device, "setFlashMode:", 1);
} else {
plus.ios.invoke(device, "setTorchMode:", 0);
plus.ios.invoke(device, "setFlashMode:", 0);
}
plus.ios.invoke(device, "unlockForConfiguration");
}
}
};
显⽰应⽤内的ViewController
// NewViewController为应⽤内创建的原⽣的ViewController类名,所调⽤页⾯的内容需要在原⽣代码中完成var newVCobj = wObject("NewViewController");
var UIApplicationClass = plus.ios.importClass("UIApplication");
var UIAppObj = UIApplicationClass.sharedApplication();
var del = plus.ios.invoke(UIAppObj,"delegate");
// 如果当前应⽤delegate对象包含UIWindow对象并且变量名命名为“window”可以这么写,
// 否则需要根据实际代码情况修改
// 应⽤的delegate对象也可以添加⼀个返回UIViewController的⽅法
var appWindowObj = plus.ios.invoke(del,"window");
var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");
plus.ios.invoke(appRootController,"presentViewController:animated:completion:",newVCobj,"YES",null);
看完这些例⼦,是不是觉得JS特别强⼤?
赶快拿去⽤起来吧!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。