创建 Cordova/Phonegap for Android 项目

                在 Eclipse Android Developer Tools 中新建 Android Application Project
本贴首发于: http://xuekaiyuan.com/forum.php?mod=viewthread&tid=8
在 Creates a new Android Application 界面中输入程序相关信息
在 Configure Project 界面中
选中 Create custom launcher icon
取消选中 Create activity
取消选中 Mark this project as a library
取消选中 Create Project in Workspace
在 Create Activity 界面中
取消选中 Create Activity

引用 Cordova 库
单击 Project 菜单,单击 Properties 菜单项
左侧选择 Android ,在右侧的 Library 中单击 Add 按钮,选择 Cordova

创建 HTML 文件
在 assets 文件夹中创建 www 文件夹,在 www 文件夹中创建 index.html 文件,输入下面的内容
    <html>    <body>    <p>Hello World!</p>    </body>    </html>

隐藏应用程序的标题栏
编辑 /res/values/styles.xml 文件,在下面的代码位置增加一行不显示应用程序的标题栏
        <style name="AppTheme" parent="AppBaseTheme">            <!-- All customizations that are NOT specific to a particular API-level can go here. -->            <item name="android:windowNoTitle">true</item>        </style>

新建 Android XML Layout File
在 File 中输入 main
在 Root Element 中选择 LinearLayout
将生成 /res/layout/main.xml 文件

编辑 /res/layout/main.xml
在编辑器中拖放 org.apache.cordova.CordovaWebView 到设计区,文件中将自动增加如下代码。
        <org.apache.cordova.CordovaWebView            android:id="@+id/cordovaWebView1"            android:layout_width="match_parent"            android:layout_height="match_parent" />
文件完整的代码如下。
    <?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <org.apache.cordova.CordovaWebView            android:id="@+id/cordovaWebView1"            android:layout_width="match_parent"            android:layout_height="match_parent" />    </LinearLayout>

创建 Activity 类
在 src 中创建包 test1.actions
在包 test1.actions 中创建类 Test1Activity
Superclass 选择 android.app.Activity
Interfaces 中 Add org.apache.cordova.api.CordovaInterface
选中 Inherited abstract methods

按 Cordova 要求实现 getActivity 函数
    @Override    public Activity getActivity() {    // TODO Auto-generated method stub    return this;    }

按 Cordova 要求实现 getThreadPool 函数
创建一个成员变量
private final ExecutorService threadPool = Executors.newCachedThreadPool();
编写 getThreadPool 函数
    @Override    public ExecutorService getThreadPool() {    // TODO Auto-generated method stub    return threadPool;    }

按 Cordova 要求绑定 WebView 对象
创建一个成员变量
private CordovaWebView cordovaWebView;
编写 onCreate 函数
    @Override    public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    cordovaWebView = (CordovaWebView) findViewById(R.id.cordovaWebView1);    cordovaWebView.loadUrl("file:///android_asset/www/index.html");    }
编写 onDestroy 函数
    @Override    public void onDestroy() {    super.onDestroy();    if (cordovaWebView != null) {    cordovaWebView.handleDestroy();    }    }

完整的程序代码如下
    package test1.actions;    import java.util.concurrent.ExecutorService;    import java.util.concurrent.Executors;    import org.apache.cordova.CordovaWebView;    import org.apache.cordova.api.CordovaInterface;    import org.apache.cordova.api.CordovaPlugin;    import test1.R;    import android.app.Activity;    import android.content.Intent;    import android.os.Bundle;    public class Test1Activity extends Activity implements CordovaInterface {                        private final ExecutorService threadPool = Executors.newCachedThreadPool();            private CordovaWebView cordovaWebView;            @Override            public void onCreate(Bundle savedInstanceState) {                    super.onCreate(savedInstanceState);                                       setContentView(R.layout.main);                                       cordovaWebView = (CordovaWebView) findViewById(R.id.cordovaWebView1);                                       cordovaWebView.loadUrl("file:///android_asset/www/index.html");            }                        @Override            public void onDestroy() {                    super.onDestroy();                    if (cordovaWebView != null) {                            cordovaWebView.handleDestroy();                    }            }                        @Override            public void startActivityForResult(CordovaPlugin command, Intent intent,                            int requestCode) {                    // TODO Auto-generated method stub            }            @Override            public void setActivityResultCallback(CordovaPlugin plugin) {                    // TODO Auto-generated method stub            }            @Override            public Activity getActivity() {                    // TODO Auto-generated method stub                    return this;            }            @Override            public Object onMessage(String id, Object data) {                    // TODO Auto-generated method stub                    return null;            }            @Override            public ExecutorService getThreadPool() {                    // TODO Auto-generated method stub                    return threadPool;            }    }

编辑 AndroidManifest.xml 文件
打开 AndroidManifest.xml 编辑器,选择 Application 选项卡
在 Application Nodes 中单击 Add 按钮,选择 Activity
选择 Activity,设置 Name 值为 .actions.Test1Activity
选择 Activity,单击 Add 按钮,选择 Intent Filter
选择 Intent Filter,单击 Add 按钮,选择 Action
选择 Action,设置 Name 值为 android.intent.action.MAIN
选择 Category,设置 Name 值为 android.intent.category.LAUNCHER
完成后在文件中将增加如下代码
            <activity android:name=".actions.Test1Activity">                <intent-filter>                    <action android:name="android.intent.action.MAIN" />                 <category android:name="android.intent.category.LAUNCHER" />                </intent-filter>            </activity>
完整的文件代码如下
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.daonao.test1"        android:versionCode="1"        android:versionName="1.0" >        <uses-sdk            android:minSdkVersion="1"            android:targetSdkVersion="17" />        <application            android:icon="@drawable/ic_launcher"            android:label="@string/app_name"            android:theme="@style/AppTheme" >            <activity android:name=".actions.Test1Activity">                <intent-filter>                    <action android:name="android.intent.action.MAIN"/>                    <category android:name="android.intent.category.LAUNCHER"/>                </intent-filter>            </activity>        </application>    </manifest>

在虚拟机中运行的效果如下

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/gdfjhc/article/details/86665083