您的位置:首页 > 编程语言 > Java开发

在NetBeans中使用PhoneGap写Android软件

2012-03-17 19:59 225 查看

最近几日突然心血来潮想玩玩编写Android的软件,好久之前也曾经考虑玩玩的,但是由于需要使用java,而本人对于java只停留在能够勉强的读相关的代码上,而且也没打算深入学习java,所以之前很快就放弃了。但是这2天发现居然有好几个用html+javascript就可以开发Android软件的中间件,其中phonegap、Rexsee都是开源的,于是又想玩玩Android了,而其中Rexsee貌似是将网页文件放在服务器上,它编译得到的apk文件仅相当于一个浏览器【当然还负责与网页的交互,以便网页可以获取手机的相关信息】,而PhoneGap是将网页文件全打包进入apk文件,因此使用PhoneGap开发的软件就不是必须要访问网络了。

几经搜寻,几乎都只有Eclipse下使用PhoneGap的教程,但是本人比较习惯使用NetBeans而不习惯Eclipse,还好NetBeans下有一个插件“nbandroid”(牛逼android? ^_^)可以在NetBeans下进行Android的开发,几经艰辛,总算在一个国外网站找到一篇关于在NetBeans下使用PhoneGap的文章【其实一开始看到一堆的英文就头晕,都没仔细看,只是后来想了一下在NetBeans下使用PhoneGap应该与Eclipse下使用PhoneGap是一致的,于是自己动手试了试,然后再会过头来仔细阅读这篇鸟文,确实是一样的】
总结:在NetBeans中使用PhoneGap的方法与《eclipse中使用PhoneGap》的方法一致。
下面附上相关的2篇文章:

《PhoneGap and Netbeans》:【以下内容来自:http://tim-perry.co.uk/blog/2010/02/20/PhoneGap_and_Netbeans】

We’ve recently been looking at writing apps for phones (it’s tough to resist a bandwagon), and we found
PhoneGap. It’s a framework that lets you write apps for most of the big phone platforms (iPhone, Android, Blackberry etc) in nothing but
HTML and JavaScript, and still get at things like the camera. I haven’t got far enough with it to really judge it yet because it’s made to be used in
Eclipse, when I actually really wanted to code in
NetBeans, and setting that up is a right hassle. After doing that, I figured the internet might want this answer too.

If you’re writing phonegap stuff it’s generally easiest to make separate projects for each platform, write the one platform and then pass out the
HTML/JS to the others, and write each native bit separately. With this in mind the following instructions are for setting up a Android project, but I imagine it’s not too difficult to work out the iPhone steps. At least, I hope so, I’m going to
have to do it at some point.

Download and set up the Android
SDK. Instructions are all on that page, you can ignore the Eclipse stuff, you need to do the platform stuff.
Download phonegap (I’m using 0.8) and extract it
Build phonegap. This didn’t work for me, so I followed the instructions
here. Essentially there are a few typos, and you need to put two semicolons in the right places.
Copy the [phonegap dir]/lib/android/phonegap.js (or min.js, or both) to [phonegap dir]/android/assets/www.
Install nbandroid, a NetBeans plugin for android projects.

By this point you’ve got all the bits you need to get everything working, and it’s just a matter of getting a project made and building in NetBeans. Essentially you make a NetBeans android project, and then splice it together with the [phonegap dir]/android
folder:

Make a new android project somewhere, through NetBeans.
Copy the res folder from phonegap/android over the res folder NetBeans just made, and build the NetBeans project. This should create a new R.java.
Copy the phonegap/android/src folder over the src folder NetBeans made.
Inside NetBeans go to the Projects window → Source Packages → com.phonegap.demo, select all the files, right click and go to Refactor → Move. Move them into the package that the rest of your project is in.
Copy the libs, assets and AndroidManifest.xml from phonegap/android to the NetBeans project.
Add the jar in the libs folder you just copied to the libraries of the NetBeans project.
Run the Android SDK Manager program and make a new Virtual Device.

At this point the project should build, and running it should start an emulator up, and eventually try and run the demo program. Unfortunately, the demo program will only come up with ‘…index.html file not found’, or similar, because NetBeans doesn’t
correctly tell the Android SDK to get the Assets from the Assets folder you copied and put the in the apk (Android app package). To fix:

In your NetBeans project open nbproject/build-impl.xml and change the line saying ‘<arg value="${asset.dir}"/>’ to ‘<arg value="${asset*s*.dir}"/>’
【注:我这没遇到问题,所以没有进行13、14的修改。】

Go into nbproject/project.properties and change ‘assets.dir=’ to ‘assets.dir=assets’. En route, add ‘assets.available=true’

Enjoy!

Eclipse中使用PhoneGap:【以下内容来自:http://www.phonegap.cn/?page_id=442#android】

1. 需求

Eclipse 3.4+

本教程还有另外一个不使用Eclipse的版本,参照这里 。【注:这里写的“另一个不使用Eclipse的版本”我一直打不开该页面,所以不知道究竟是什么玩意】

2. 安装SDK + PhoneGap



下载并安装Eclipse Classic



下载并安装Android SDK



下载并安装ADT Plugin



下载 最新版本的PhoneGap并解压,我们将使用其中的Android目录。【注:官网http://phonegap.com/,我写这个时最新版是1.5.0】

3. 新项目设置

启动Eclipse,然后在菜单“File”下选择“New > Android Project”



在项目根目录下,创建两个新目录:

/libs
/assets/www

复制phonegap.js(从PhoneGap解压缩后的Android目录中,将解压缩后的带版本号的js文件名修改为phonegap.js)到/assets/www。
【注:1.5.0版中不是phonegap.js而是cordova-1.5.0.js】

复制phonegap.jar(从PhoneGap解压缩后的Android目录中)到/libs。
【注:1.5.0版中不是phonegap.jar而是cordova-1.5.0.jar】

复制xml整个目录(从PhoneGap解压缩后的Android目录中,包括一个plugins.xml)到/res。【v1.0 rc2以上】
对Eclipse的src文件夹中的主要Java文件进行少量调整:(如下图)

将class的继承由Activity改为DroidGap
setContentView()替换为super.loadUrl("file:///android_asset/www/index.html");
添加import com.phonegap.*; 【1.5.0中应为:import org.apache.cordova.*;】


移除import android.app.Activity;



在这里你可能会遇到Eclipse找不到phonegap-1.0.0.jar的错误。在这种情况下,右键单击/libs文件夹找到Build Paths/ > Configure Build Paths。然后在Libraries标签页中添加phonegap-1.0.0.jar到项目中。如果Eclipse不是很稳定,你需要点击F5刷新一次项目。
右键单击AndroidManifest.xml并选择Open With > Text Editor
将下面的权限设置拷贝到versionName之后:(如下图) 【注:这个可以在PhoneGap内的sample中找到AndroidManifest.xml后从中复制】

<supports-screens

android:largeScreens="true"

android:normalScreens="true"

android:smallScreens="true"

android:resizeable="true"

android:anyDensity="true"

/>

<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.RECEIVE_SMS" />

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.WRITE_CONTACTS" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />




在AndroidManifest的activity标签中添加
android:configChanges="orientation|keyboardHidden"
。(如上图)

4. Hello World

“/assets/www”目录中新建文件“index.html”,并粘贴如下代码:

<!DOCTYPE HTML>

<html>

<head>

<title>PhoneGap</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>

</head>

<body>

<h1>Hello World</h1>

</body>

</html>


5A. 部署到模拟器

右键单击项目节点选择Run As,然后点击Android Application
Eclipse将要求你选择一个合适的AVD,如果没有设置的话,你需要创建一个AVD。

5B. 部署到设备

确认设备已经勾选USB debugging选项,并已将设备连接到电脑。(设置 > 应用程序 > 开发)
右键单击项目节点选择Run As,点击Android Application

完成!

你可以在这里了解本指南的详细版本。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: