在 Cordova/Phonegap for Android 中调用 API
编辑 AndroidManifest.xml
选择 Permissions 选项卡
单击 Add 按钮,选择 Uses Permission,在 Name 属性中选择 android.permission.ACCESS_NETWORK_STATE
完整的文件代码如下
复制 Cordova 项目中 assets/www/cordova.js 文件到当前项目的 assets/www/cordova.js 位置
编辑 assets/www/index.html 文件
完整的文件代码如下
在虚拟机中运行的效果如下
在《创建 Cordova/Phonegap for Android 项目》http://xuekaiyuan.com/forum.php?mod=viewthread&tid=8 后不能直接调用Cordova/Phonegap 提供的各种 API ,需要引用 cordova.js 封装好的 API,cordova.js 和程序之间通讯还需要 ACCESS_NETWORK_STATE 权限
本贴首发于:http://xuekaiyuan.com/forum.php?mod=viewthread&tid=10
编辑 AndroidManifest.xml
选择 Permissions 选项卡
单击 Add 按钮,选择 Uses Permission,在 Name 属性中选择 android.permission.ACCESS_NETWORK_STATE
完整的文件代码如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.daonao.test3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".actions.Test3Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
复制 Cordova 项目中 assets/www/cordova.js 文件到当前项目的 assets/www/cordova.js 位置
编辑 assets/www/index.html 文件
完整的文件代码如下
<html>
<head>
<title>Hello World from Test3!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script>
var deviceInfo = function () {
var platform = document.createElement("DIV");
platform.innerText = "platform: " + device.platform;
document.body.appendChild(platform);
var version = document.createElement("DIV");
version.innerText = "version: " + device.version;
document.body.appendChild(version);
var uuid = document.createElement("DIV");
uuid.innerText = "uuid: " + device.uuid;
document.body.appendChild(uuid);
var model = document.createElement("DIV");
model.innerText = "model: " + device.model;
document.body.appendChild(model);
var width = document.createElement("DIV");
width.innerText = "width: " + screen.width;
document.body.appendChild(width);
var height = document.createElement("DIV");
height.innerText = "height: " + screen.height;
document.body.appendChild(height);
var colorDepth = document.createElement("DIV");
colorDepth.innerText = "colorDepth: " + screen.colorDepth;
document.body.appendChild(colorDepth);
}
function init() {
document.addEventListener("deviceready", deviceInfo, true);
}
</script>
</head>
<body onload="init();">
</body>
</html>
在虚拟机中运行的效果如下