`

PhoneGap的Android端插件开发

 
阅读更多

来自51cto:http://mobile.51cto.com/android-309311.htm

 

前面一篇文章 《移动 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. * Starts the video player intent
    8. *
    9. * @param url The url to play
    10. */
    11. VideoPlayer.prototype.play = function(url) {
    12. PhoneGap.exec(null, null, "VideoPlayer", "playVideo", [url]);
    13. };
    14. /**
    15. * Load VideoPlayer
    16. */
    17. PhoneGap.addConstructor(function() {
    18. PhoneGap.addPlugin("videoPlayer", new VideoPlayer());
    19. });
  3. 编写 Android VideoPlayer 的具体实现代码,VideoPlayer/src/com/phonegap/plugins/video/VideoPlayer.java
    1. package com.phonegap.plugins.video;
    2. import org.json.JSONArray;
    3. import org.json.JSONException;
    4. import android.content.Intent;
    5. import android.net.Uri;
    6. import com.phonegap.api.Plugin;
    7. import com.phonegap.api.PluginResult;
    8. publicclass VideoPlayer extends Plugin {
    9. privatestaticfinal String YOU_TUBE = "youtube.com";
    10. @Override
    11. public PluginResult execute(String action, JSONArray args, String callbackId) {
    12. PluginResult.Status status = PluginResult.Status.OK;
    13. String result = "";
    14. try {
    15. if (action.equals("playVideo")) {
    16. playVideo(args.getString(0));
    17. }
    18. else {
    19. status = PluginResult.Status.INVALID_ACTION;
    20. }
    21. returnnew PluginResult(status, result);
    22. } catch (JSONException e) {
    23. returnnew PluginResult(PluginResult.Status.JSON_EXCEPTION);
    24. }
    25. }
    26. privatevoid playVideo(String url) {
    27. // Create URI
    28. Uri uri = Uri.parse(url);
    29. Intent intent = null;
    30. // Check to see if someone is trying to play a YouTube page.
    31. if (url.contains(YOU_TUBE)) {
    32. // If we don't do it this way you don't have the option for youtube
    33. intent = new Intent(Intent.ACTION_VIEW, uri);
    34. } else {
    35. // Display video player
    36. intent = new Intent(Intent.ACTION_VIEW);
    37. intent.setDataAndType(uri, "video/*");
    38. }
    39. this.ctx.startActivity(intent);
    40. }
    41. }
  4. 配置插件, res/xml/plugins.xml 添加如下代码
    1. <pluginname="VideoPlayer"value="com.phonegap.plugins.video.VideoPlayer"/>
  5. 编写代码进行调用,文件开头引入js代码框架,然后进行VideoPlayer类的play函数调用
    1. <scripttype="text/javascript"charset="utf-8"src="phonegap.js"></script>
    2. <scripttype="text/javascript"charset="utf-8"src="video.js"></script>
    3. //Sample use:
    4. /**
    5. * Display an intent to play the video.
    6. *
    7. * @param url The url to play
    8. */
    9. //play(url)
    10. window.plugins.videoPlayer.play("http://path.to.my/video.mp4");
    11. window.plugins.videoPlayer.play("file:///path/to/my/video.mp4");
  6. 到此为止,插件的开发和部署,以及调用就都ok了,是不是很简单啊!

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

【编辑推荐】

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics