PhoneGap for Android平台源码的示例分析
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,今天就跟大家聊聊有关PhoneGap for Android平台源码的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。PhoneGap内
千家信息网最后更新 2025年11月07日PhoneGap for Android平台源码的示例分析
今天就跟大家聊聊有关PhoneGap for Android平台源码的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
PhoneGap内部数据传递使用了JSON,比如PhoneGap中指南针部分源码如下:
public class CompassListener extends Plugin implements SensorEventListener { public static int STOPPED = 0; public static int STARTING = 1; public static int RUNNING = 2; public static int ERROR_FAILED_TO_START = 3; public long TIMEOUT = 30000; // Timeout in msec to shut off listener int status; // status of listener float heading; // most recent heading value long timeStamp; // time of most recent value long lastAccessTime; // time the value was last retrieved private SensorManager sensorManager;// Sensor manager Sensor mSensor; // Compass sensor returned by sensor manager /** * Constructor. */ public CompassListener() { this.timeStamp = 0; this.setStatus(CompassListener.STOPPED); } /** * Sets the context of the Command. This can then be used to do things like * get file paths associated with the Activity. * * @param ctx The context of the main Activity. */ public void setContext(PhonegapActivity ctx) { super.setContext(ctx); this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); } /** * Executes the request and returns PluginResult. * * @param action The action to execute. * @param args JSONArry of arguments for the plugin. * @param callbackId The callback id used when calling back into JavaScript. * @return A PluginResult object with a status and message. */ public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult.Status status = PluginResult.Status.OK; String result = ""; try { if (action.equals("start")) { this.start(); } else if (action.equals("stop")) { this.stop(); } else if (action.equals("getStatus")) { int i = this.getStatus(); return new PluginResult(status, i); } else if (action.equals("getHeading")) { // If not running, then this is an async call, so don't worry about waiting if (this.status != RUNNING) { int r = this.start(); if (r == ERROR_FAILED_TO_START) { return new PluginResult(PluginResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START); } // Wait until running long timeout = 2000; while ((this.status == STARTING) && (timeout > 0)) { timeouttimeout = timeout - 100; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } if (timeout == 0) { return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START); } } float f = this.getHeading(); return new PluginResult(status, f); } else if (action.equals("setTimeout")) { this.setTimeout(args.getLong(0)); } else if (action.equals("getTimeout")) { long l = this.getTimeout(); return new PluginResult(status, l); } return new PluginResult(status, result); } catch (JSONException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.JSON_EXCEPTION); } } /** * Identifies if action to be executed returns a value and should be run synchronously. * * @param action The action to execute * @return T=returns value */ public boolean isSynch(String action) { if (action.equals("getStatus")) { return true; } else if (action.equals("getHeading")) { // Can only return value if RUNNING if (this.status == RUNNING) { return true; } } else if (action.equals("getTimeout")) { return true; } return false; } /** * Called when listener is to be shut down and object is being destroyed. */ public void onDestroy() { this.stop(); } //-------------------------------------------------------------------------- // LOCAL METHODS //-------------------------------------------------------------------------- /** * Start listening for compass sensor. * * @return status of listener */ public int start() { // If already starting or running, then just return if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) { return this.status; } // Get accelerometer from sensor manager List list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION); // If found, then register as listener if (list.size() > 0) { this.mSensor = list.get(0); this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL); this.lastAccessTime = System.currentTimeMillis(); this.setStatus(CompassListener.STARTING); } // If error, then set status to error else { this.setStatus(CompassListener.ERROR_FAILED_TO_START); } return this.status; } /** * Stop listening to compass sensor. */ public void stop() { if (this.status != CompassListener.STOPPED) { this.sensorManager.unregisterListener(this); } this.setStatus(CompassListener.STOPPED); } public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } /** * Sensor listener event. * * @param SensorEvent event */ public void onSensorChanged(SensorEvent event) { // We only care about the orientation as far as it refers to Magnetic North float heading = event.values[0]; // Save heading this.timeStamp = System.currentTimeMillis(); this.heading = heading; this.setStatus(CompassListener.RUNNING); // If heading hasn't been read for TIMEOUT time, then turn off compass sensor to save power if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) { this.stop(); } } /** * Get status of compass sensor. * * @return status */ public int getStatus() { return this.status; } /** * Get the most recent compass heading. * * @return heading */ public float getHeading() { this.lastAccessTime = System.currentTimeMillis(); return this.heading; } /** * Set the timeout to turn off compass sensor if getHeading() hasn't been called. * * @param timeout Timeout in msec. */ public void setTimeout(long timeout) { this.TIMEOUT = timeout; } /** * Get the timeout to turn off compass sensor if getHeading() hasn't been called. * * @return timeout in msec */ public long getTimeout() { return this.TIMEOUT; } /** * Set the status and send it to JavaScript. * @param status */ private void setStatus(int status) { this.status = status; } } 看完上述内容,你们对PhoneGap for Android平台源码的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
源码
内容
平台
示例
分析
指南
指南针
数据
更多
知识
篇文章
行业
资讯
资讯频道
部分
频道
进一
支持
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
唐山企业管理软件开发团队
剑侠世界3手游服务器通
放弃软件开发后怎么样
国家网络安全整顿
京东方网络安全顾问
网络安全课堂观后感500
戴尔机架服务器改机箱
我注销抖音号怎么显示服务器错误
网安网络安全保卫工作重点
数据库 码和属性
什么网易服务器最好
怎么删除电脑dbe数据库
网络安全方向有哪些部门
萤火虫软件开发
网络安全校园主题日班会
服务器硬件维保有前途吗
尖峰软件开发有限公司
网络安全业务内容包括
第二届网络安全精英赛
法院里面的网络安全维护工作
戴尔服务器配置ip
神武互通版卡在获取服务器
中国银行软件开发中心16
联硕宁波互联网科技有限公司
网络安全方向有哪些部门
网络安全短视频封面
软件开发芯片产业园
顺德软件开发项目管理
即墨区安卓软件开发系统
奥迪德国总部新软件开发中心