文章来自: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目录;

改动界面;

改动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 ]);

弹出提示

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>Cordoca</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" 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 http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
-->
<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 World</title> </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;

简单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

android:Cordova Android, hello Cordova ,PhoneGap android的更多相关文章

  1. Phonegap Android 项目使用Cordova

    要在已经创建好的Android项目里,使用Cordova. 1. 首先在Android Studio中创建Android项目 2. 创建cordova项目 cordova crate test com ...

  2. ionic2 自定义cordova插件开发以及使用 (Android)

    如何写一个cordova 用于ionic2项目中呢,在搜索了一番之后,千篇一律,我都怀疑那些文章是不是全部都是复制来复制去的,而且都不是很详细.我自己也捣鼓了一下午,踩了很多坑.所以特此写这下这篇,记 ...

  3. Cordova项目config.xml添加android权限

    最近在开发cordova项目,安卓APP需要调用照相机和系统相册,在添加安卓权限的时候,总是报错. 以下是部分config.xml代码 <platform name="android& ...

  4. Android H5混合开发(3):原生Android项目里嵌入Cordova

    前言 如果安卓项目已经存在了,那么如何使用Cordova做混合开发? 方案1(适用于插件会持续增加或变化的项目): 新建Cordova项目并添加Android平台,把我们的安卓项目导入Android平 ...

  5. 原生Android项目里嵌入Cordova

    Android H5混合开发():原生Android项目里嵌入Cordova 如果安卓项目已经存在了,那么如何使用Cordova做混合开发? 方案1(适用于插件会持续增加或变化的项目): 新建Cord ...

  6. 如何在原生Android项目里嵌入Cordova

    背景: 这段时间在维护一个Cordova混合项目,以前稍微接触过Cordova,也写过简单的纯纯的Cordova的Demo,但是没有尝试过混合原生的Cordova. 在接到项目后比较了一下项目架构和C ...

  7. 新手的第一个phonegap Android应用

    对PhoneGap开发感兴趣的请加入群 PhoneGap App开发 348192525   手机成为现在软件应用必不可少的一种设备,然而手机平台的不统一造成我们需要为不同手机重写代码,这对一般应用来 ...

  8. PhoneGap Android环境搭建

    原文地址:http://www.cnblogs.com/shawn-xie/archive/2012/08/15/2638480.html 一.安装 在安装PhoneGap开发环境之前,需要按顺序安装 ...

  9. phonegap android 输入法弹出会遮盖表单框的解决办法

    phonegap android 当页面内容比较多,表单超出屏幕范围时,点击输入,输入法会遮盖住表单框,而且无法向上滑动. 经过测试发现,是由于config.xml中设置了 FullScreen 的全 ...

随机推荐

  1. java的struts2整理

    readbook:struts2 先说一下struts1:    struts1使用model II 模式开发,即jsp+java bean+servlet    再说它的缺陷:    1.表现层支持 ...

  2. clips 前端 js 动画 抛物线加入购物车

    抛物线加入购物车的特效动画(支持ie7以上,移动端表现良好)    1.引用一个极小的jquery插件库    2.启动 设置 起点 终点 和完成后回调函数 1.插件地址 git-hub上的官方主页 ...

  3. 当表名存在 点 的时候,EntityFramework的写法

    原文发布时间为:2011-05-17 -- 来源于本人的百度文章 [由搬家工具导入] 需要在 Context 中,重写 DbContext 中的 OnModelCreating 方法 进行 mappi ...

  4. input 输入框 光标错位问题 、移动端输入框/input框光标错位问题、微信H5输入框/input框光标错位问题

    在IOS系统下的问题: 搜索出的建议如下: 你应该是用fixed定位做的弹出框,弹出框里面有文本框.fixed在ios上兼容不友好,会造成光标乱跳.建议用better-scroll,或者用absolu ...

  5. MongoDB中$redact操作符的使用

    介绍 redact <- redaction 修订/校验,意思是对文档内容进行过滤,选择一些过滤或保留的信息 . access level 存取等级 有三种: - $$DESCEND 返回当前等 ...

  6. 关于boostrapValidator动态添加字段(addField)验证的bug

    每次码博客,都觉得自己怀才不遇,哎~脑袋有瑕疵,文笔拿不粗手,就直接上干货吧. 在使用boostrapValidator这个验证插件的时候,如果某一个字段是动态添加来的,我们需要调用方法:addFie ...

  7. qt include无法自动补齐

    原因一: 检查cmake有没有include_directories 原因二: CmakeList.txt放错位置 原因三: 没有用qt new file来创建头文件

  8. logstash filter plugin

    1. 基本语法%{NUMBER:duration} %{IP:client} 2. 支持的数据类型默认会把所有的匹配都当作字符串,比如0.043, 想要转成浮点数,可以%{NUMBER:num:flo ...

  9. Listener监听器笔记1

    1.常用的Web事件监听器接口: 1.ServletContextListener:用于监听Web应用的启动和关闭. 2.ServletContextAttributeListener:用于监听Ser ...

  10. [NOIP模拟赛][并没有用二分][乱搞AC]

    圆圈舞蹈 [问题描述] 熊大妈的奶牛在时针的带领下,围成了一个圆圈跳舞.由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针走,到达 ...