phoneGap-Android開發環境搭建
參考 http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html
0.jdk
java -version
javac -version
如何上面的某個指令不能運作(找不到),就必須配置環境變量。
配置環境變量
java_home="C:\Program Files\Java\jdk1.8.0_05"
classpath='.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar'
path =%java_home%\bin;%path%
1.安裝adt;
2.下載下傳phonegap包
http://phonegap.com/install/
2.1.安裝nodejs和phonegap/cordova
nodejs
npm list -g
[email protected]
[email protected]
npm install -g phonegap 安裝
2.2.phonegap archive下載下傳
裡面有我們要用到的3個檔案和檔案夾
但是很悲劇2.9.1修改了
沒有phonegap.js, 有cordova.js(就用這個好了,不過據說有差別,要看官方文檔)
沒有cordova.jar,有源代碼
方法一:
将“phonegap-2.9.1\lib\android\framework\src”裡面的“com”和“org”檔案夾複制到項目的“src”檔案夾下,就可以了。
方法二:
下載下傳“cordova-2.9.1.jar”,http://download.csdn.net/detail/workceo/7279415
或者你熟悉一點點java的話,就可以自己動手:
把framework裡面src目錄下面的兩個源代碼編譯為class檔案再做成jar包就好了,其實就是一個java項目的build為jar的過程了。
npm root -g cordova 安裝路徑
npm update cordova更新
npm help install
npm help update
3.adt中使用phonegap
3.0.adt安裝插件
3.0.1.GEF
3.0.2.SVN
3.0.3.apatana3
3.0.4.http://www.7edown.com/downpage.asp?id=26278
安裝相應插件:
1.安裝wtp,友善web開發
打開菜單help->Install new Software,單擊“add..”按鈕,彈出框的name和location都填入“http://download.eclipse.org/webtools/updates/”,等待一會,勾選“Web Tools Platform (WTP) 3.1.2”
2.安裝“java decompiler”
打開菜單help->Install new Software,單擊“add..”按鈕,彈出框的name和location都填入“http://java.decompiler.free.fr/jd-eclipse/update”,等待一會,勾選“Java Decompiler Eclipse Plug-in”,按向導進行安裝。
相關配置:
1.統一設定為utf-8編碼,Window->Preferences->General->Workspace->Text file encoding->Other->UTF-8。
2.修改jsp等預設編碼為utf-8,打開Window->Preferences->Web->Jsp Files,修改右面的encoding為(utf-8)。
3.統一設定為utf-8編碼,Window->Preferences->General->Content Types,在右面選擇“Text”,在default encoding輸入“UTF-8”,點“update”按鈕更新。
經過配置後,所有的建立的java、jsp、txt、html、xml、檔案夾等編碼都是utf-8,這樣在jsp、html等檔案中設定編碼為utf-8,可以避免亂碼。
3.1.new an android project
3.2. add cordova.jar to lib folder
3.3. add a new folder "www" under assets(here we place cordova.js and index.html)
我們可以參考phonegap包裡面的example和framework目錄
3.4.修改android項目的配置檔案AndroidManifest.xml
3.4.1. AndroidManifest.xml檔案中加入相應的supports-screens,uses-permission和uses-feature。
3.4.2.将以下内容添加到AndroidManifest.xml檔案的activity标簽中:
android:configChanges="orientation|keyboardHidden
這是為了保證機器在橫豎屏切換的時候不會重新執行Activity的onCreate方法;
最後看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellophonegap"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true" />
<!-- android:xlargeScreens="true" screen supported only after Android-9 -->
<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.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<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-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hellophonegap.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3.5.
.最後再修改下src下的Java主檔案(如果沒有就自己建立一個),我們要做以下幾件事:
1)添加import com.phonegap.*; 2)删掉import android.app.Activity; 3)修改MainActivity的基類,這裡将MainActivity繼承為DroidGap; 4)onCreate()方法
修改onCreate()方法為public
把setContentView()這行替換為super.loadUrl("file:///android_asset/www/index.html"); 5)最後看起來就像這樣:
package com.example.hellophonegap;
//import android.app.Activity;
import org.apache.cordova.DroidGap;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
super.loadUrl("file:///android_asset/www/index.html");
// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}