PhoneGap for Android平台源码示例

移动开发 Android
前一段,介绍了PhoneGap开源项目用于多平台的学习在《PhoneGap让你的应用兼容Android、iOS、WP7 》一文中讲到了,今天一起来看下PhoneGap for Android平台的代码吧, 对于初入Android开发的网友可以很好的了解代码规范,同时很多成熟的可复用代码希望对大家有帮助。

PhoneGap内部数据传递使用了JSON,比如PhoneGap中指南针部分源码如下:

  1. public class CompassListener extends Plugin implements SensorEventListener { 
  2.  
  3.  public static int STOPPED = 0
  4.  public static int STARTING = 1
  5.     public static int RUNNING = 2
  6.     public static int ERROR_FAILED_TO_START = 3
  7.     
  8.     public long TIMEOUT = 30000;  // Timeout in msec to shut off listener 
  9.   
  10.     int status;       // status of listener 
  11.     float heading;      // most recent heading value 
  12.     long timeStamp;      // time of most recent value 
  13.     long lastAccessTime;    // time the value was last retrieved 
  14.   
  15.     private SensorManager sensorManager;// Sensor manager 
  16.     Sensor mSensor;      // Compass sensor returned by sensor manager 
  17.   
  18.  /** 
  19.   * Constructor. 
  20.   */ 
  21.  public CompassListener() { 
  22.         this.timeStamp = 0
  23.         this.setStatus(CompassListener.STOPPED); 
  24.  } 
  25.  
  26.  /** 
  27.   * Sets the context of the Command. This can then be used to do things like 
  28.   * get file paths associated with the Activity. 
  29.   * 
  30.   * @param ctx The context of the main Activity. 
  31.   */ 
  32.  public void setContext(PhonegapActivity ctx) { 
  33.   super.setContext(ctx); 
  34.         this.sensorManager = (SensorManager) ctx.getSystemService(Context.SENSOR_SERVICE); 
  35.  } 
  36.  
  37.  /** 
  38.   * Executes the request and returns PluginResult. 
  39.   * 
  40.   * @param action   The action to execute. 
  41.   * @param args    JSONArry of arguments for the plugin. 
  42.   * @param callbackId The callback id used when calling back into JavaScript. 
  43.   * @return     A PluginResult object with a status and message. 
  44.   */ 
  45.  public PluginResult execute(String action, JSONArray args, String callbackId) { 
  46.   PluginResult.Status status = PluginResult.Status.OK; 
  47.   String result = "";   
  48.    
  49.   try { 
  50.    if (action.equals("start")) { 
  51.     this.start(); 
  52.    } 
  53.    else if (action.equals("stop")) { 
  54.     this.stop(); 
  55.    } 
  56.    else if (action.equals("getStatus")) { 
  57.     int i = this.getStatus(); 
  58.     return new PluginResult(status, i); 
  59.    } 
  60.    else if (action.equals("getHeading")) { 
  61.     // If not running, then this is an async call, so don't worry about waiting 
  62.     if (this.status != RUNNING) { 
  63.      int r = this.start(); 
  64.      if (r == ERROR_FAILED_TO_START) { 
  65.       return new PluginResult(PluginResult.Status.IO_EXCEPTION, ERROR_FAILED_TO_START); 
  66.      } 
  67.      // Wait until running 
  68.      long timeout = 2000
  69.      while ((this.status == STARTING) && (timeout > 0)) { 
  70.       timeouttimeout = timeout - 100; 
  71.       try { 
  72.        Thread.sleep(100); 
  73.       } catch (InterruptedException e) { 
  74.        e.printStackTrace(); 
  75.       } 
  76.      } 
  77.      if (timeout == 0) { 
  78.       return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START);       
  79.      } 
  80.     } 
  81.     float f = this.getHeading(); 
  82.     return new PluginResult(status, f); 
  83.    } 
  84.    else if (action.equals("setTimeout")) { 
  85.     this.setTimeout(args.getLong(0)); 
  86.    } 
  87.    else if (action.equals("getTimeout")) { 
  88.     long l = this.getTimeout(); 
  89.     return new PluginResult(status, l); 
  90.    } 
  91.    return new PluginResult(status, result); 
  92.   } catch (JSONException e) { 
  93.    e.printStackTrace(); 
  94.    return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 
  95.   } 
  96.  } 
  97.  
  98.  /** 
  99.   * Identifies if action to be executed returns a value and should be run synchronously. 
  100.   * 
  101.   * @param action The action to execute 
  102.   * @return   T=returns value 
  103.   */ 
  104.  public boolean isSynch(String action) { 
  105.   if (action.equals("getStatus")) { 
  106.    return true; 
  107.   } 
  108.   else if (action.equals("getHeading")) { 
  109.    // Can only return value if RUNNING 
  110.    if (this.status == RUNNING) { 
  111.     return true; 
  112.    } 
  113.   } 
  114.   else if (action.equals("getTimeout")) { 
  115.    return true; 
  116.   } 
  117.   return false; 
  118.  } 
  119.     
  120.     /** 
  121.      * Called when listener is to be shut down and object is being destroyed. 
  122.      */ 
  123.  public void onDestroy() { 
  124.   this.stop(); 
  125.  } 
  126.  
  127.     //-------------------------------------------------------------------------- 
  128.     // LOCAL METHODS 
  129.     //-------------------------------------------------------------------------- 
  130.  
  131.     /** 
  132.      * Start listening for compass sensor. 
  133.      * 
  134.      * @return    status of listener 
  135.      */ 
  136.  public int start() { 
  137.    
  138.   // If already starting or running, then just return 
  139.         if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) { 
  140.          return this.status; 
  141.         } 
  142.  
  143.   // Get accelerometer from sensor manager 
  144.   List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION); 
  145.  
  146.         // If found, then register as listener 
  147.   if (list.size() > 0) { 
  148.    this.mSensor = list.get(0); 
  149.    this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL); 
  150.             this.lastAccessTime = System.currentTimeMillis(); 
  151.             this.setStatus(CompassListener.STARTING); 
  152.   } 
  153.  
  154.   // If error, then set status to error 
  155.         else { 
  156.             this.setStatus(CompassListener.ERROR_FAILED_TO_START); 
  157.         } 
  158.         
  159.         return this.status; 
  160.  } 
  161.   
  162.     /** 
  163.      * Stop listening to compass sensor. 
  164.      */ 
  165.  public void stop() { 
  166.         if (this.status != CompassListener.STOPPED) { 
  167.          this.sensorManager.unregisterListener(this); 
  168.         } 
  169.         this.setStatus(CompassListener.STOPPED); 
  170.  } 
  171.   
  172.   
  173.  public void onAccuracyChanged(Sensor sensor, int accuracy) { 
  174.   // TODO Auto-generated method stub  
  175.  } 
  176.  
  177.     /** 
  178.      * Sensor listener event. 
  179.      * 
  180.      * @param SensorEvent event 
  181.      */ 
  182.  public void onSensorChanged(SensorEvent event) { 
  183.  
  184.   // We only care about the orientation as far as it refers to Magnetic North 
  185.   float heading = event.values[0]; 
  186.  
  187.   // Save heading 
  188.         this.timeStamp = System.currentTimeMillis(); 
  189.   this.heading = heading; 
  190.   this.setStatus(CompassListener.RUNNING); 
  191.  
  192.   // If heading hasn't been read for TIMEOUT time, then turn off compass sensor to save power 
  193.   if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) { 
  194.    this.stop(); 
  195.   } 
  196.  } 
  197.   
  198.     /** 
  199.      * Get status of compass sensor. 
  200.      * 
  201.      * @return   status 
  202.      */ 
  203.  public int getStatus() { 
  204.   return this.status; 
  205.  } 
  206.   
  207.  /** 
  208.   * Get the most recent compass heading. 
  209.   * 
  210.   * @return   heading 
  211.   */ 
  212.  public float getHeading() { 
  213.         this.lastAccessTime = System.currentTimeMillis(); 
  214.   return this.heading; 
  215.  } 
  216.   
  217.  /** 
  218.   * Set the timeout to turn off compass sensor if getHeading() hasn't been called. 
  219.   * 
  220.   * @param timeout  Timeout in msec. 
  221.   */ 
  222.  public void setTimeout(long timeout) { 
  223.   this.TIMEOUT = timeout
  224.  } 
  225.   
  226.  /** 
  227.   * Get the timeout to turn off compass sensor if getHeading() hasn't been called. 
  228.   * 
  229.   * @return timeout in msec 
  230.   */ 
  231.  public long getTimeout() { 
  232.   return this.TIMEOUT; 
  233.  } 
  234.  
  235.  /** 
  236.   * Set the status and send it to JavaScript. 
  237.   * @param status 
  238.   */ 
  239.  private void setStatus(int status) { 
  240.   this.status = status; 
  241.  } 
  242.  

 

责任编辑:佚名 来源: Android123
相关推荐

2011-09-14 09:20:03

PhonegapAndroid平台

2011-09-05 15:09:06

Android平台Phonegap

2012-03-07 11:17:19

AndroidPhoneGap插件

2012-02-01 10:40:28

PhoneGap 1.

2012-07-06 13:50:44

跨平台工具Adobe Phone

2011-08-31 13:27:52

AndroidPhoneGap

2011-07-22 08:34:37

PhoneGapSymbian

2011-07-15 15:54:38

PhoneGapiOS

2011-07-19 08:50:17

PhoneGapwebOS

2011-12-21 21:34:50

PhoneGapiOSAndroid

2011-12-23 10:02:37

PhoneGapAndroid插件

2011-07-01 15:28:26

PhoneGap代码示例

2011-07-18 14:46:56

PhoneGapBlackBerry

2010-10-09 15:01:27

PhoneGapiPhoneAndroid

2011-09-02 13:30:43

Android SDKPhoneGap

2011-12-23 09:53:24

PhoneGap

2012-03-07 11:30:09

PhoneGapWindows Pho

2011-12-30 15:11:36

Adobe视频PhoneGap

2011-09-13 13:36:17

PhoneGap

2011-07-05 15:26:23

点赞
收藏

51CTO技术栈公众号