文章来自:http://blog.csdn.net/intbird

官方文档:

http://cordova.apache.org/docs/en/5.0.0//index.html

intbird的俩DEMO:

https://github.com/intbird/cordova-android_cordova4.0.0.git
https://github.com/intbird/cordova-android_cordova3.7.2.git

下载安装nodeJS:

https://nodejs.org/

下载安装cordova:

http://cordova.apache.org/docs/en/5.0.0//guide_cli_index.md.html#The%20Command-Line%20Interface

$ sudo npm install -g cordova

创建项目:

$ cordova create hello com.example.hello HelloWorld

添加平台支持:

$ cordova platform add ios$ cordova platform add amazon-fireos$ cordova platform add android$ cordova platform add blackberry10$ cordova platform add firefoxos

Android Studio导入项目运行OK.

一路next;可以安装官方 guides 导入android文件夹;
android:Cordova Android, hello Cordova ,PhoneGap android_第1张图片

修改界面;

修改assert下 index.html,添加两个button

<body onload="onPageLoad()">    <input type="button" onclick="btnStartActivity('web')" value="使用webView" class="button"/><br/>    <input type="button" onclick="btnStartActivity('camera')" value="使用相机" class="button"/><br/>    <input type="button" onclick="btnStartActivity('')" value="未处理" class="button"/>    body>

添加js事件 方式一,方式二文章末尾;

note: exec 中的第三个参数为res下config.xml文件配置的功能名称;
修改index.js

function onPageLoad(){    document.addEventListener("deviceready", handle, false);}function handle(){     console.log("deviceready");     document.addEventListener("resume",onResume,false);}function onResume(){    showToast("onResume");}function btnStartActivity(arg){    var calssName = "";    switch(arg){        case 'web':        calssName = "com.intbird.soft.cordovar.WebViewActivity"            break;        case 'camera':        calssName = "com.intbird.soft.cordovar.CameraActivity";            break;        default:        showToast('ERROR');            break;    }    if("" != calssName)        cordova.exec(resultSuccess,resultError,"CallActivityPlugin","call",[calssName]);}function resultSuccess(result){    showToast("success "+result);}function resultError(error){    showToast("error "+error);}function showToast(message){    cordova.exec(null, null, "ToastPlugin", "toast", [ message ]);}

Plugin插件编写;

http://cordova.apache.org/docs/en/5.0.0//guide_platforms_android_plugin.md.html#Android%20Plugins
如上面的
cordova.exec(null, null, “ToastPlugin”, “toast”, [ message ]);

android:Cordova Android, hello Cordova ,PhoneGap android_第2张图片

弹出提示

package com.intbird.soft.cordoca.plugins;import android.widget.Toast;import org.apache.cordova.CallbackContext;import org.apache.cordova.CordovaPlugin;import org.json.JSONArray;import org.json.JSONException;/** * Created by intbird on 15/6/11. */public class ToastPlugin extends CordovaPlugin {    public static final String  ACTION = "toast";    @Override    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {        if (action.equals(ACTION)) {            Toast.makeText(cordova.getActivity(), args.getString(0), Toast.LENGTH_SHORT).show();        }        return true;    }}

调用Activity

package com.intbird.soft.cordoca.plugins;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import org.apache.cordova.CallbackContext;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.PluginResult;import org.json.JSONArray;import org.json.JSONException;/** * Created by intbird on 15/6/11. */public class CallActivityPlugin extends CordovaPlugin {    public static final String  ACTION = "call";    @Override    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {        if(this.cordova.getActivity().isFinishing()) return true;        if(action.equals(ACTION)){            try{                Intent intent = new Intent(cordova.getActivity(),Class.forName(args.getString(0)));                this.cordova.startActivityForResult(this,intent,-1);                PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);                mPlugin.setKeepCallback(true);                callbackContext.sendPluginResult(mPlugin);                callbackContext.success("activity started");                callbackContext.error("activity not start.");            }catch(Exception ex){                ex.printStackTrace();                return  false;            }        }        return true;    }    @Override    public void onActivityResult(int requestCode, int resultCode, Intent intent) {        switch (resultCode){            case Activity.RESULT_OK:                Bundle b = intent.getExtras();                String str = b.getString("changge01");                break;            default:                break;        }    }}

配置插件;

在 res/xml/config.xml中添加feature配置,同时修改背景色和其他标志

   <preference name="ShowTitle" value="true"/>    <preference name="BackgroundColor" value="0x00FFFFFF"/>
<?xml version='1.0' encoding='utf-8'?><widget id="com.intbird.soft.cordoca" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">    <preference name="loglevel" value="DEBUG" />    <allow-intent href="market:*" />    <name>Cordocaname>    <description>        A sample Apache Cordova application that responds to the deviceready event.    description>    <author email="[email protected]" href="http://cordova.io">        Apache Cordova Team    author>    <content src="index.html" />    <access origin="*" />    <allow-intent href="http://*/*" />    <allow-intent href="https://*/*" />    <allow-intent href="tel:*" />    <allow-intent href="sms:*" />    <allow-intent href="mailto:*" />    <allow-intent href="geo:*" />    feature name="Whitelist">        <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />        <param name="onload" value="true" />    feature>    <feature name="ToastPlugin">        <param name="android-package" value="com.intbird.soft.cordoca.plugins.ToastPlugin" />    feature>    <feature name="CallActivityPlugin">        <param name="android-package" value="com.intbird.soft.cordoca.plugins.CallActivityPlugin" />    feature>widget>

报错;

06-11 17:47:24.708 12895-12895/com.intbird.soft.cordoca I/chromium﹕ [INFO:CONSOLE(41)] “Refused to execute inline event handler because it violates the following Content Security Policy directive: “default-src ‘self’ data: gap: https://ssl.gstatic.com ‘unsafe-eval’”. Note that ‘script-src’ was not explicitly set, so ‘default-src’ is used as a fallback.
“, source: file:///android_asset/www/index.html (41)

06-11 17:47:24.708  12895-12895/com.intbird.soft.cordoca I/chromium﹕ [INFO:CONSOLE(41)] "Refused to execute inline event handler because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.    ", source: file:///android_asset/www/index.html (41)

直接注释掉 index.html meta 第一行,重新运行;

<html>    <head>                <meta name="format-detection" content="telephone=no">        <meta name="msapplication-tap-highlight" content="no">        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">        <link rel="stylesheet" type="text/css" href="css/index.css">        <script type="text/javascript" charset="utf-8" src="cordova.js">script>        <script type="text/javascript" charset="utf-8" src="js/index.js">script>        <title>Hello Worldtitle>    head>    <body onload="onPageLoad()">    <input type="button" onclick="btnStartActivity('web')" value="使用webView" class="button"/><br/>    <input type="button" onclick="btnStartActivity('camera')" value="使用相机" class="button"/><br/>    <input type="button" onclick="btnStartActivity('')" value="未处理" class="button"/>    body>html>

AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.intbird.soft.cordovar"    android:hardwareAccelerated="true"    android:versionCode="1"    android:versionName="0.0.1">    <supports-screens        android:anyDensity="true"        android:largeScreens="true"        android:normalScreens="true"        android:resizeable="true"        android:smallScreens="true"        android:xlargeScreens="true" />    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />    <application        android:hardwareAccelerated="true"        android:icon="@drawable/icon"        android:label="@string/app_name"        android:supportsRtl="true">        <activity            android:name="MainActivity"            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"            android:label="@string/activity_name"            android:launchMode="singleTop"            android:theme="@android:style/Theme.Holo.Light"            android:windowSoftInputMode="adjustResize">            <intent-filter android:label="@string/launcher_name">                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            intent-filter>        activity>        <activity            android:name=".CameraActivity"            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"            android:label="@string/activity_name"            android:launchMode="singleTop"            android:theme="@android:style/Theme.Holo.Light"            android:windowSoftInputMode="adjustResize">activity>        <activity            android:name="WebViewActivity"            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"            android:label="@string/activity_name"            android:launchMode="singleTop"            android:theme="@android:style/Widget.WebView"            android:windowSoftInputMode="adjustResize">activity>    application>    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="22" />manifest>

运行点击未处理默认提示,相机和webview见demo;

android:Cordova Android, hello Cordova ,PhoneGap android_第3张图片

简单demo副本.http://download.csdn.net/detail/intbird/8797289

添加js事件方式二:

将插件函数独立出来在指定js文件中
修改前:
function showToast(message){
cordova.exec(null, null, “ToastPlugin”, “toast”, [ message ]);
}
修改后:
function showToast(message){
navigator.window.show(null, null, “ToastPlugin”, “toast”, [ message ]);

1.创建assert/www/plugins/intbird-plugins-package
2,在1文件夹内添加 showtoast.js文件;

cordova.define("intbird-plugins-package.showtoast", function(require, exports, module) {var exec = require('cordova/exec');var showToast = function(message) {    exec(null,null,"ToastPlugin","toast",[message]);};module.exports = showToast;});

3,配置插件文件cordova_plugins,
cordova_plugins.js文件

{        "file": "plugins/intbird-plugin-package/showtoast.js",        "id": "intbird-plugins-package.showtoast",        "clobbers": [            "window.showToast"        ]    }module.exports.metadata = // TOP OF METADATA{    "cordova-plugin-whitelist": "1.0.0",    "intbird-plugins-package": "1.0.0",}

界面中使用
window.showToast(“Toast message”);
//note: “clobbers”: [ “window.showToast”]

demo:https://github.com/intbird/cordova-android-demo.git
文章来自 :http://blog.csdn.net/intbird

更多相关文章

  1. android 下载文件注意事项
  2. android添加桌面快捷方式
  3. mainfest文件中android属性
  4. ~/.dirlst 文件
  5. Android 读取和保存文件(手机内置存储器)
  6. Android——文件存储
  7. Android layout文件中 '?' 的作用
  8. :如何安装apk文件在Android仿真器中
  9. android基础知识03——事件处理01:主要事件及其处理方式

随机推荐

  1. Android(安卓)Studio打包时出现transform
  2. Android自定义对话框(Dialog)位置,大小
  3. Android(安卓)animation - 文字旋转示例
  4. android launch 初探
  5. Android适配底部虚拟键盘遮挡布局的解决
  6. android仿滴滴司机端滑动接到乘客,送达乘
  7. Android(安卓)获取MIEI ISMI Sim卡串号等
  8. Android(安卓)各种Dialog例子
  9. Android(安卓)各种Context区别
  10. Android(安卓)实现旋转键盘的例子