`
CoderDream
  • 浏览: 478535 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

PhoneGap学习笔记之01 HelloWorld

阅读更多

教材《Apress Beginning PhoneGap Mobile Web Framework for JavaScript and HTML5》

 

假定我们已经搭建好了Android的环境,AVD版本为2.3.1

 

下面开始创建PhoneGap的HelloWorld项目:

 

Create a New Project 创建一个新项目

Step 1: Create an Android Project 第一步:创建一个Android项目
Open Eclipse, click on File->New Project->Android Project. 

打开Eclipse,点击 File->New Project->Android Project,



 

This is shown in the following steps:

步骤如下:
1. Put ABPG-HelloWorld as the project name.输入项目名称
2. Enter HelloWorld as the application name. 应用程序的名称
3. Enter org.examples.phonegap.sample as the package name. 唯一的包名

4. Ensure that you have selected Android 2.3.3 as the build target. 编译的版本



 
5. Check the Create Activity checkbox and enter HelloWorldActivity as the activity name. The activity in Android is a screen. And the activity name is also the class name of the activity. Activity的名称

 

Step 2: Add PhoneGap Libraries to the Project 将PhoneGap的库文件加入项目中
Once the Android Project is created, it's time to inject the PhoneGap framework into the Android Project. As we have mentioned before, PhoneGap comes with three main components: the native component, the XML plugin, and a JavaScript file.
1. To install the native component in Android, create a directory named libs in the project and copy the PhoneGap jar into it. You can either drag and drop the phonegap-1.1.0.jar in the libs folder, or you can copy and paste it into the libs folder in the Eclipse IDE. Next, add the PhoneGap jar to the class path by right clicking Build Path -> Add to Build Path.

将phonegap-1.1.0.jar拷贝到【libs】文件夹中,如果没有这个文件夹,则需要创建,然后把这个jar加到编译路径中:

 

 


2. Copy the XML directory from the PhoneGap’s Android Directory into the res folder. 将【xml】文件夹拷贝到项目的【res】文件夹下:

【xml】文件夹的路径为:X:\XXX\phonegap-1.1.0\Android\xml\

3. Once the PhoneGap Jar Is added to the Android Project, it’s time to inject the JavaScript file of the PhoneGap into the project. We will create a www folder under the Assets Folder of the Android Project. The Assets Folder is like the media folder of the Android Application. In our case, we will put all of the files of the browser-based application inside of the www folder. To begin with, add the PhoneGap JavaScript file to the www folder, found in the Assets Folder.

在【assets】文件夹下创建【www】文件夹,这个文件夹会创建所有基于浏览器的文件,如html,js,css等等,首先将 phonegap-1.1.0.js 文件拷贝到该文件夹中,然后创建index.html文件

Step 3: Modify Android Permissions 修改Android访问权限
In Android applications, the main file is the Android Manifest file. In this file there are many specific things, like the package name, which uniquely identify the application on the market. The main file contains a section called permissions. Android uses this section to inform the user that the application will be using certain features of the phone.
Say an application intends to use the Internet to fetch data; permission needs to be attained in order to install the application. When the user installs the application he will be shown by the Android market that this application will be given the permission to use the Internet.

 

Android market that this application will be given the permission to use the Internet.
For the PhoneGap, the following permissions need to be added:
1. Add the following permissions to the Android Manifest XML: 增加下面的权限
<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" />


2. We will also need to add the supports-screen option in the Manifest file, as seen as follows:屏幕选项
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />


3. Add android:configChanges=orignetation|keyboardHidden to the activity in the Android Manifest. This tells the Android not to kill and recreate the activity when the user flips the phone and the screen switches from portrait to landscape and vice versa.

 


4. Add a second activity after the previous one, by applying the following XML snippet:
<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter> </intent-filter>
</activity>

 

完整版:
Once you have modified the Android Manifest, as per the previous instructions, an Android Manifest XML will appear. It will be seen as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.examples.phonegap.helloworld" android:versionCode="1" android:versionName="1.0">
<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" />
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="HelloWorld" android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.phonegap.DroidGap"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>


Step 4: Modify the Main Activity
In Android, a class named activity represents a screen. In order for us to use the
PhoneGap in the Android, we will change the screen from an activity to a DroidGap.
DroidGap is a special activity, which allows us to show HTML pages.
NOTE: We are telling the DroidGap to load the index.html file in the Android Assets.

package com.abpg.helloworld;

import android.os.Bundle;
import android.view.Menu;

import com.phonegap.DroidGap;

public class HelloWorldActivity extends DroidGap {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.loadUrl("file:///android_asset/www/index.html");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.hello_world, menu);
		return true;
	}

}

 
Write the HelloWorld Application
A PhoneGap application is an HTML/JavaScript application.
Following is the index.html.
1. Include the PhoneGap JavaScript Library version 1.1.0 in the HTML page.
2. Register the init() method with the body’s onload event.
3. In the init() function, register the JavaScript callback function onDeviceReady
with the deviceready event.
4. In the onDeviceReady callback function, change the contents of the h1 element
with the ID“helloworld” with the text “hello World! Loaded PhoneGap Framework!”
The complete source code is listed here:

<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" src="phonegap-1.1.0.js"></script>
<script type="text/javascript">
/** Called when phonegap javascript is loaded */
function onDeviceReady(){
document.getElementById("helloworld").innerHTML
="Hello World! Loaded PhoneGap Framework!";
}
/** Called when browser load this page*/
function init(){
document.addEventListener("deviceready", onDeviceReady, false);
}
</script>
</head>
<body onLoad="init()">
<h1 id="helloworld">...</h1>
</body>
</html>

 

 

  • 大小: 44.3 KB
  • 大小: 37.7 KB
  • 大小: 355.4 KB
  • 大小: 122.8 KB
  • 大小: 110 KB
分享到:
评论

相关推荐

    phonegap-start, PhoneGap Hello World 应用程序.zip

    phonegap-start, PhoneGap Hello World 应用程序 Hello World 应用程序 使用PhoneGap构建的Hello World 应用程序用法桌面在浏览器中,打开 file: /www/index.html CLI当你创建一个新应用程序时,这

    ios phonegap2.9 helloworld

    这个"ios phonegap2.9 helloworld"项目是一个入门级别的教程,旨在帮助初学者理解如何在iOS平台上使用PhoneGap 2.9版本创建基本的应用程序。PhoneGap 2.9是该框架的一个旧版本,但在当时仍然广泛使用,因此了解其...

    PhoneGap HelloWorld

    PhoneGap HelloWorld是一个基于Android平台,使用PhoneGap开源框架开发的应用示例。PhoneGap是一个非常流行的跨平台移动应用开发工具,它允许开发者使用HTML、CSS和JavaScript来构建原生的移动应用,而无需深入学习...

    PhoneGapHelloWorld

    phonegap create HelloWorld --id "com.example.helloworld" ``` 接下来,进入项目目录并启动本地开发服务器: ``` cd HelloWorld phonegap serve ``` 项目结构通常包含以下几个关键文件和目录: 1. `index....

    phonegap学习资料

    ### PhoneGap 学习资料详解 #### 一、PhoneGap 概述 **PhoneGap** 是一个移动开发框架,允许开发者使用 **HTML**、**JavaScript** 和 **CSS** 开发跨平台的移动应用程序。该技术最早由 **Nitobi Software** 公司在...

    PhoneGap学习[归纳].pdf

    - Hello World:编写简单的"Hello World"应用,展示PhoneGap的基本用法。 - 部署:将应用部署到模拟器或实际设备上进行测试,包括在模拟器上运行以检查功能,以及在真实设备上测试性能和用户体验。 4. 难度评估 ...

    phonegap之android示例

    在这个示例中,“HelloWorld”很可能就是应用的主入口,它包含了展示"Hello, World!"的基本结构和逻辑,这是学习任何新平台或技术时的传统起点。 HTML(HyperText Markup Language)是用于创建网页的标准标记语言,...

    android之PhoneGap入门实例

    为了使这个入门实例更具实用性,我们可以扩展HelloWorld,添加PhoneGap的API调用来访问设备特性。例如,可以添加一个按钮,点击后获取设备的唯一标识符,或者显示一个弹出框提示用户获取地理位置信息。这需要在...

    phonegap项目环境搭建

    在命令行中,导航到你想要创建项目的目录,然后运行以下命令来创建一个新的PhoneGap项目,名为“helloworld”: ```bash phonegap create helloworld ``` 这将创建一个包含基本结构的项目文件夹,包括`www`目录,...

    Phonegap Helloworld

    PhoneGap 最基本配置,直接运行《 Helloword

    Android Phone Gap入门程序(Phone Gap HelloWorld)

    PhoneGap HelloWorld是一个基础的Android应用开发项目,它利用了PhoneGap框架来构建。PhoneGap是一个开源的开发平台,允许开发者使用HTML、CSS和JavaScript来创建跨平台的移动应用程序,覆盖了包括Android在内的多个...

    phoneGAP2.9.1.zip

    - **熟悉的开发环境**:对于熟悉Web开发的程序员来说,PhoneGap使用HTML、CSS和JavaScript,这意味着他们无需学习新的编程语言。 - **设备API访问**:通过PhoneGap,Web应用可以访问手机硬件功能,如相机、联系人...

    IONIC+PhoneGap项目源码

    【IONIC】 ...IONIC的设计理念是“移动...综合以上,这个压缩包提供了一个使用IONIC和PhoneGap开发的项目源码,以及关于PhoneGap的深入学习材料,对于想要学习或提升混合移动应用开发技能的开发者来说,是非常宝贵的资源。

    android上phonegap例子

    在这个"android上phonegap例子"中,我们看到的是一个名为"HelloWorld"的基本项目,这是大多数编程教程中的传统入门示例。这个例子将帮助开发者理解如何在Android设备上搭建和运行一个基本的PhoneGap应用。 1. **...

    android与phonegap混合开发之相互跳转

    在移动应用开发领域,PhoneGap和Android是两种广泛使用的平台。PhoneGap是一种基于HTML5、CSS3和JavaScript的跨平台框架,它允许开发者使用Web技术创建原生移动应用。而Android则是Google主导的开源操作系统,提供了...

    phonegap demo

    在"phonegap demo"项目中,"helloworld"通常是最基础的入门示例。这个示例可能包含以下几个关键部分: 1. **index.html**:这是应用的主页面,使用HTML编写,展示应用的用户界面。你可以看到HTML元素,如`&lt;head&gt;`和...

    phonegap完整例子!

    PhoneGap是一个开源框架,它允许开发者使用HTML5、CSS3和JavaScript来构建跨平台的移动应用。这个"phonegap完整例子!"是针对Android平台的一个示例项目,...这些内容为深入学习和实践PhoneGap开发提供了全面的指导。

    phonegap源码+示例

    总之,这份PhoneGap源码和示例对于希望深入学习PhoneGap或移动跨平台开发的开发者来说是一份宝贵的学习资料。它不仅让我们能够理解PhoneGap的工作原理,还能通过示例掌握实际开发技巧,提升我们的跨平台开发能力。

Global site tag (gtag.js) - Google Analytics