zoukankan      html  css  js  c++  java
  • PhoneGap本地将html打包成安卓App

    PhoneGap的在线打包有大小限制,超过30M的包无法在线打包。当然,可以把包里面的图片、声音文件去掉,然后打包。下载以后,解包,重新打包并签名。蛮麻烦的。

    本地打包的简单方法如下:

    下载安装Java环境。

    下载安装ADT。http://developer.android.com/sdk/index.html

    打开ADT,新建一个安卓应用项目

    输入名称啥的,然后就可以一路下一步

    可以选择下项目位置,我的是默认的。

    这里可以选择图标。

    选择第一个

    这个时候,一个安卓项目就建好了。这个时候运行,会看到默认的样子,不管他,无视。

    将PhoneGap目录下的android目录下的jar文件拷贝到项目的libs目录下

    将xml目录拷贝到项目的res目录下

    在assetc目录下,建立一个www目录,下面放html内容。为了偷懒,我把phonegap例子里面的内容拷贝过来了。

    修改Java代码:

    [java] view plain copy
     
    1. package com.myexample.helloworld;  
    2.   
    3. import android.os.Bundle;  
    4. import org.apache.cordova.*;  
    5.   
    6. public class MainActivity extends DroidGap  
    7. {  
    8.     @Override  
    9.     public void onCreate(Bundle savedInstanceState)  
    10.     {  
    11.         super.onCreate(savedInstanceState);  
    12.         // Set by <content src="index.html" /> in config.xml  
    13.         super.loadUrl(Config.getStartUrl());  
    14.         //super.loadUrl("file:///android_asset/www/index.html")  
    15.     }  
    16. }  
    17.   
    18. /* 
    19.  * 下面是adt生成的代码,注释掉 
    20. import android.os.Bundle; 
    21. import android.app.Activity; 
    22. import android.view.Menu; 
    23.  
    24. public class MainActivity extends Activity { 
    25.  
    26.     @Override 
    27.     protected void onCreate(Bundle savedInstanceState) { 
    28.         super.onCreate(savedInstanceState); 
    29.         setContentView(R.layout.activity_main); 
    30.     } 
    31.  
    32.     @Override 
    33.     public boolean onCreateOptionsMenu(Menu menu) { 
    34.         // Inflate the menu; this adds items to the action bar if it is present. 
    35.         getMenuInflater().inflate(R.menu.main, menu); 
    36.         return true; 
    37.     } 
    38.  
    39. }*/  


    修改一下项目根目录下的AndroidManifest.xml和res/xml目录下的config.xml文件

    AndroidManifest.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <!--  
    3.        Licensed to the Apache Software Foundation (ASF) under one  
    4.        or more contributor license agreements.  See the NOTICE file  
    5.        distributed with this work for additional information  
    6.        regarding copyright ownership.  The ASF licenses this file  
    7.        to you under the Apache License, Version 2.0 (the  
    8.        "License"); you may not use this file except in compliance  
    9.        with the License.  You may obtain a copy of the License at  
    10.   
    11.          http://www.apache.org/licenses/LICENSE-2.0  
    12.   
    13.        Unless required by applicable law or agreed to in writing,  
    14.        software distributed under the License is distributed on an  
    15.        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
    16.        KIND, either express or implied.  See the License for the  
    17.        specific language governing permissions and limitations  
    18.        under the License.  
    19.   
    20. -->  
    21. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    22.     package="com.myexample.helloworld"  
    23.     android:hardwareAccelerated="true"  
    24.     android:versionCode="1"  
    25.     android:versionName="1.0"  
    26.     android:windowSoftInputMode="adjustPan" >  
    27.   
    28.     <supports-screens  
    29.         android:anyDensity="true"  
    30.         android:largeScreens="true"  
    31.         android:normalScreens="true"  
    32.         android:resizeable="true"  
    33.         android:smallScreens="true"  
    34.         android:xlargeScreens="true" />  
    35.   
    36.     <uses-permission android:name="android.permission.CAMERA" />  
    37.     <uses-permission android:name="android.permission.VIBRATE" />  
    38.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    39.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    40.     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />  
    41.     <uses-permission android:name="android.permission.INTERNET" />  
    42.     <uses-permission android:name="android.permission.RECEIVE_SMS" />  
    43.     <uses-permission android:name="android.permission.RECORD_AUDIO" />  
    44.     <uses-permission android:name="android.permission.RECORD_VIDEO" />  
    45.     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />  
    46.     <uses-permission android:name="android.permission.READ_CONTACTS" />  
    47.     <uses-permission android:name="android.permission.WRITE_CONTACTS" />  
    48.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    49.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    50.     <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
    51.     <uses-permission android:name="android.permission.BROADCAST_STICKY" />  
    52.   
    53.     <application  
    54.         android:debuggable="true"  
    55.         android:hardwareAccelerated="true"  
    56.         android:icon="@drawable/ic_launcher"  
    57.         android:label="@string/app_name" >  
    58.         <activity  
    59.             android:name="com.myexample.helloworld.MainActivity"  
    60.             android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"  
    61.             android:label="@string/app_name"  
    62.             android:theme="@android:style/Theme.Black.NoTitleBar" >  
    63.             <intent-filter>  
    64.                 <action android:name="android.intent.action.MAIN" />  
    65.   
    66.                 <category android:name="android.intent.category.LAUNCHER" />  
    67.             </intent-filter>  
    68.         </activity>  
    69.     </application>  
    70.   
    71.     <uses-sdk  
    72.         android:minSdkVersion="7"  
    73.         android:targetSdkVersion="17" />  
    74.   
    75. </manifest>  


    config.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!--  
    3.  Licensed to the Apache Software Foundation (ASF) under one  
    4.  or more contributor license agreements.  See the NOTICE file  
    5.  distributed with this work for additional information  
    6.  regarding copyright ownership.  The ASF licenses this file  
    7.  to you under the Apache License, Version 2.0 (the  
    8.  "License"); you may not use this file except in compliance  
    9.  with the License.  You may obtain a copy of the License at  
    10.   
    11.  http://www.apache.org/licenses/LICENSE-2.0  
    12.   
    13.  Unless required by applicable law or agreed to in writing,  
    14.  software distributed under the License is distributed on an  
    15.  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
    16.  KIND, either express or implied.  See the License for the  
    17.  specific language governing permissions and limitations  
    18.  under the License.  
    19. -->  
    20. <widget  
    21.     id="com.myexample.helloworld"  
    22.     version="2.0.0"  
    23.     xmlns="http://www.w3.org/ns/widgets" >  
    24.   
    25.     <name>  
    26. helloworld  
    27.     </name>  
    28.   
    29.     <description>  
    30.         A sample Apache Cordova application that responds to the deviceready event.  
    31.     </description>  
    32.   
    33.     <author  
    34.         email="dev@cordova.apache.org"  
    35.         href="http://cordova.io" >  
    36.         Apache Cordova Team  
    37.     </author>  
    38.   
    39.     <access origin="*" />  
    40.   
    41.     <!-- <content src="http://mysite.com/myapp.html" /> for external pages -->  
    42.     <content src="index.html" />  
    43.   
    44.     <preference  
    45.         name="loglevel"  
    46.         value="DEBUG" />  
    47.     <!--  
    48.       <preference name="splashscreen" value="resourceName" />  
    49.       <preference name="backgroundColor" value="0xFFF" />  
    50.       <preference name="loadUrlTimeoutValue" value="20000" />  
    51.       <preference name="InAppBrowserStorageEnabled" value="true" />  
    52.       <preference name="disallowOverscroll" value="true" />  
    53.     -->  
    54.   
    55.     <feature name="App" >  
    56.         <param  
    57.             name="android-package"  
    58.             value="org.apache.cordova.App" />  
    59.     </feature>  
    60.     <feature name="Geolocation" >  
    61.         <param  
    62.             name="android-package"  
    63.             value="org.apache.cordova.GeoBroker" />  
    64.     </feature>  
    65.     <feature name="Device" >  
    66.         <param  
    67.             name="android-package"  
    68.             value="org.apache.cordova.Device" />  
    69.     </feature>  
    70.     <feature name="Accelerometer" >  
    71.         <param  
    72.             name="android-package"  
    73.             value="org.apache.cordova.AccelListener" />  
    74.     </feature>  
    75.     <feature name="Compass" >  
    76.         <param  
    77.             name="android-package"  
    78.             value="org.apache.cordova.CompassListener" />  
    79.     </feature>  
    80.     <feature name="Media" >  
    81.         <param  
    82.             name="android-package"  
    83.             value="org.apache.cordova.AudioHandler" />  
    84.     </feature>  
    85.     <feature name="Camera" >  
    86.         <param  
    87.             name="android-package"  
    88.             value="org.apache.cordova.CameraLauncher" />  
    89.     </feature>  
    90.     <feature name="Contacts" >  
    91.         <param  
    92.             name="android-package"  
    93.             value="org.apache.cordova.ContactManager" />  
    94.     </feature>  
    95.     <feature name="File" >  
    96.         <param  
    97.             name="android-package"  
    98.             value="org.apache.cordova.FileUtils" />  
    99.     </feature>  
    100.     <feature name="NetworkStatus" >  
    101.         <param  
    102.             name="android-package"  
    103.             value="org.apache.cordova.NetworkManager" />  
    104.     </feature>  
    105.     <feature name="Notification" >  
    106.         <param  
    107.             name="android-package"  
    108.             value="org.apache.cordova.Notification" />  
    109.     </feature>  
    110.     <feature name="Storage" >  
    111.         <param  
    112.             name="android-package"  
    113.             value="org.apache.cordova.Storage" />  
    114.     </feature>  
    115.     <feature name="FileTransfer" >  
    116.         <param  
    117.             name="android-package"  
    118.             value="org.apache.cordova.FileTransfer" />  
    119.     </feature>  
    120.     <feature name="Capture" >  
    121.         <param  
    122.             name="android-package"  
    123.             value="org.apache.cordova.Capture" />  
    124.     </feature>  
    125.     <feature name="Battery" >  
    126.         <param  
    127.             name="android-package"  
    128.             value="org.apache.cordova.BatteryListener" />  
    129.     </feature>  
    130.     <feature name="SplashScreen" >  
    131.         <param  
    132.             name="android-package"  
    133.             value="org.apache.cordova.SplashScreen" />  
    134.     </feature>  
    135.     <feature name="Echo" >  
    136.         <param  
    137.             name="android-package"  
    138.             value="org.apache.cordova.Echo" />  
    139.     </feature>  
    140.     <feature name="Globalization" >  
    141.         <param  
    142.             name="android-package"  
    143.             value="org.apache.cordova.Globalization" />  
    144.     </feature>  
    145.     <feature name="InAppBrowser" >  
    146.         <param  
    147.             name="android-package"  
    148.             value="org.apache.cordova.InAppBrowser" />  
    149.     </feature>  
    150.     <!-- Deprecated plugins element. Remove in 3.0 -->  
    151.     <plugins>  
    152.     </plugins>  
    153.   
    154. </widget>  


    然后,就可以运行了

    PhoneGap的官方方法不是这样的,是用命令行生成默认包的。但是要装好几个东西。具体可以看PhoneGap包里面的readme文档。

  • 相关阅读:
    apache的并发
    PHP 文件上传
    打包备份3天
    dz改写CSS
    linux cifs自动挂载远程windows硬盘或文件夹
    C++面向对象
    "i++"和"++i"
    《程序员面试宝典》一个程序
    《程序员面试宝典》强制转换,内存地址
    《程序员面试宝典》编程技巧--位运算
  • 原文地址:https://www.cnblogs.com/deepbreath/p/5144775.html
Copyright © 2011-2022 走看看