PhoneGap的Android端插件开发

移动开发 Android
鉴于PhoneGap才刚刚新起,还有许多功能因为平台的差异性无法很好的解决,所以我们在实际的开发中,发现有很多功能还需要完善,一种比较好的方式就是编写平台依赖的插件,进而扩展PhoneGap的功能。

前面一篇文章 《移动 APP 之跨平台解决方案》 介绍了一种跨平台的解决方案,即用开发web app的方式来编写mobile app。鉴于PhoneGap才刚刚新起,还有许多功能因为平台的差异性无法很好的解决,所以我们在实际的开发中,发现有很多功能还需要完善,一种比较好 的方式就是编写平台依赖的插件,进而扩展PhoneGap的功能。

本文介绍一下开发和使用插件的一个流程,以 VideoPlayer 为例。

  1. 环境搭建,下载 phonegap-android 的源码,下载地址 https://github.com/phonegap/phonegap-android
  2. 编写video.js,提供给web开发端的接口定义,定义了一个VideoPlayer类和play函数,参数为要播放的文件视频地址,代码如下:
    1. /** 
    2.  * Constructor 
    3.  */ 
    4. function VideoPlayer() { 
    5. }; 
    6.  
    7. /** 
    8.  * Starts the video player intent 
    9.  * 
    10.  * @param url           The url to play 
    11.  */ 
    12. VideoPlayer.prototype.play = function(url) { 
    13.     PhoneGap.exec(nullnull"VideoPlayer""playVideo", [url]); 
    14. }; 
    15.  
    16. /** 
    17.  * Load VideoPlayer 
    18.  */ 
    19. PhoneGap.addConstructor(function() { 
    20.     PhoneGap.addPlugin("videoPlayer"new VideoPlayer()); 
    21. }); 
  3. 编写 Android VideoPlayer 的具体实现代码,VideoPlayer/src/com/phonegap/plugins/video/VideoPlayer.java
    1. package com.phonegap.plugins.video; 
    2.  
    3. import org.json.JSONArray; 
    4. import org.json.JSONException; 
    5. import android.content.Intent; 
    6. import android.net.Uri; 
    7. import com.phonegap.api.Plugin; 
    8. import com.phonegap.api.PluginResult; 
    9.  
    10. public class VideoPlayer extends Plugin { 
    11.     private static final String YOU_TUBE = "youtube.com"
    12.  
    13.     @Override 
    14.     public PluginResult execute(String action, JSONArray args, String callbackId) { 
    15.         PluginResult.Status status = PluginResult.Status.OK; 
    16.         String result = ""
    17.  
    18.         try { 
    19.             if (action.equals("playVideo")) { 
    20.                 playVideo(args.getString(0)); 
    21.             } 
    22.             else { 
    23.                 status = PluginResult.Status.INVALID_ACTION; 
    24.             } 
    25.             return new PluginResult(status, result); 
    26.         } catch (JSONException e) { 
    27.             return new PluginResult(PluginResult.Status.JSON_EXCEPTION); 
    28.         } 
    29.     } 
    30.  
    31.     private void playVideo(String url) { 
    32.         // Create URI 
    33.         Uri uri = Uri.parse(url); 
    34.  
    35.         Intent intent = null
    36.         // Check to see if someone is trying to play a YouTube page. 
    37.         if (url.contains(YOU_TUBE)) { 
    38.             // If we don't do it this way you don't have the option for youtube 
    39.             intent = new Intent(Intent.ACTION_VIEW, uri); 
    40.         } else { 
    41.             // Display video player 
    42.             intent = new Intent(Intent.ACTION_VIEW); 
    43.             intent.setDataAndType(uri, "video/*"); 
    44.         } 
    45.  
    46.         this.ctx.startActivity(intent); 
    47.     } 
  4. 配置插件, res/xml/plugins.xml 添加如下代码
    1. <plugin name="VideoPlayer" value="com.phonegap.plugins.video.VideoPlayer"/> 
  5. 编写代码进行调用,文件开头引入js代码框架,然后进行VideoPlayer类的play函数调用
    1. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
    2. <script type="text/javascript" charset="utf-8" src="video.js"></script> 
    3.  
    4.  //Sample use: 
    5.  /** 
    6.     * Display an intent to play the video. 
    7.     * 
    8.     * @param url           The url to play 
    9.     */ 
    10.  //play(url) 
    11.  
    12. window.plugins.videoPlayer.play("http://path.to.my/video.mp4"); 
    13. window.plugins.videoPlayer.play("file:///path/to/my/video.mp4"); 
  6. 到此为止,插件的开发和部署,以及调用就都ok了,是不是很简单啊!

最后向大家推荐一本书籍《PhoneGap Beginner’s Guide》,相信通过本书的学习,就知道了怎样利用PhoneGap来开发跨平台的mobile app了,同时也可以关注https://github.com/phonegap项目的最新进展情况和新特性,如果可以的话,贡献自己的力量来进行完善和扩充!

责任编辑:佚名 来源: 润物无声的博客
相关推荐

2011-10-11 10:06:12

PhoneGap插件

2012-03-07 11:17:19

AndroidPhoneGap插件

2011-09-02 13:38:56

PhoneGap插件Android

2011-07-05 15:26:23

2011-09-13 09:49:59

PhoneGap插件

2011-12-30 15:11:36

Adobe视频PhoneGap

2011-07-01 15:02:53

PhoneGap移动开发框架

2011-09-05 14:26:43

PhoneGap插件

2011-12-14 11:38:42

PhoneGapJavaAndroid

2011-12-19 08:57:46

PhoneGapNativeContr

2011-07-05 17:29:53

PhoneGapevents

2011-07-19 13:26:50

iPhone PhoneGap 框架

2011-08-31 13:11:53

AndroidPhoneGap

2012-03-07 15:07:54

PhoneGapAndroid源码示例

2011-08-31 13:27:52

AndroidPhoneGap

2011-12-15 09:45:21

PhoneGap

2011-12-22 19:57:38

PhoneGap

2011-09-02 14:06:38

PhoneGapEclipseAndroid

2014-07-04 09:43:22

2011-09-13 13:36:17

PhoneGap
点赞
收藏

51CTO技术栈公众号