在 Cordova/Phonegap for Android 中调用 API

本文介绍如何在Cordova/Phonegap for Android项目中正确配置并使用Cordova提供的API。通过添加必要的网络状态权限及引入cordova.js,实现设备信息获取等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 Cordova/Phonegap for Android 中调用 API

在《创建 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>

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值