javascript - phonegap/cordova onDeviceReady() 不触发 android 4.1.2

标签 javascript android cordova

我已经束手无策了。我正在使用 Adob​​e Build 和 PhoneGap 版本 3.5.0 以及 Cordova 3.4.0。

无论我做什么,我的 onDeviceReady() 事件都不会在我的 Android 设备上触发,无论是作为 APK 还是在 PhoneGap 开发者应用程序中。

这是我的 config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.ourwalmart" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
  <name>xxxx</name>
  <description>
xxxx  </description>
  <author email="xx@xxx.org" href="http://xxxxxxx.org">
    OUR Walmart
  </author>
  <preference name="permissions" value="none" />
  <preference name="phonegap-version" value="3.5.0" />
  <preference name="orientation" value="default" />
  <preference name="target-device" value="universal" />
  <preference name="fullscreen" value="true" />
  <preference name="webviewbounce" value="false" />
  <preference name="prerendered-icon" value="true" />
  <preference name="stay-in-webview" value="false" />
  <!--<preference name="ios-statusbarstyle" value="black-translucent" />-->
  <preference name="detect-data-types" value="true" />
  <preference name="exit-on-suspend" value="false" />
  <!--<preference name="show-splash-screen-spinner" value="true" />
  <preference name="auto-hide-splash-screen" value="true" />-->
  <preference name="disable-cursor" value="false" />
  <preference name="android-minSdkVersion" value="7" />
  <preference name="android-installLocation" value="auto" />
  <gap:plugin name="com.phonegap.plugin.statusbar" />
  <gap:plugin name="org.apache.cordova.battery-status" />
  <gap:plugin name="org.apache.cordova.camera" />
  <gap:plugin name="org.apache.cordova.media-capture" />
  <gap:plugin name="org.apache.cordova.console" />
  <gap:plugin name="org.apache.cordova.contacts" />
  <gap:plugin name="org.apache.cordova.device" />
  <gap:plugin name="org.apache.cordova.device-motion" />
  <gap:plugin name="org.apache.cordova.device-orientation" />
  <gap:plugin name="org.apache.cordova.dialogs" />
  <gap:plugin name="org.apache.cordova.file" />
  <gap:plugin name="org.apache.cordova.file-transfer" />
  <gap:plugin name="org.apache.cordova.geolocation" />
  <gap:plugin name="org.apache.cordova.globalization" />
  <gap:plugin name="org.apache.cordova.inappbrowser" />
  <gap:plugin name="org.apache.cordova.media" />
  <gap:plugin name="org.apache.cordova.network-information" />
  <gap:plugin name="org.apache.cordova.splashscreen" />
  <gap:plugin name="org.apache.cordova.vibration" />
  <icon src="icon.png" />
   <icon gap:platform="android" gap:qualifier="ldpi" src="res/icon/android/icon-36-ldpi.png" />
    <icon gap:platform="android" gap:qualifier="mdpi" src="res/icon/android/icon-48-mdpi.png" />
    <icon gap:platform="android" gap:qualifier="hdpi" src="res/icon/android/icon-72-hdpi.png" />
    <icon gap:platform="android" gap:qualifier="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
    <icon gap:platform="blackberry" src="res/icon/blackberry/icon-80.png" />
    <icon gap:platform="blackberry" gap:state="hover" src="res/icon/blackberry/icon-80.png" />
    <icon gap:platform="ios" height="57" src="res/icon/ios/icon-57.png" width="57" />
    <icon gap:platform="ios" height="72" src="res/icon/ios/icon-72.png" width="72" />
    <icon gap:platform="ios" height="114" src="res/icon/ios/icon-57-2x.png" width="114" />
    <icon gap:platform="ios" height="144" src="res/icon/ios/icon-72-2x.png" width="144" />
    <icon gap:platform="webos" src="res/icon/webos/icon-64.png" />
    <icon gap:platform="winphone" src="res/icon/windows-phone/icon-48.png" />
    <icon gap:platform="winphone" gap:role="background" src="res/icon/windows-phone/icon-173.png" /> 
  <gap:splash gap:platform="android" gap:qualifier="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png" />
  <gap:splash gap:platform="android" gap:qualifier="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png" />
  <gap:splash gap:platform="android" gap:qualifier="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png" />
  <gap:splash gap:platform="android" gap:qualifier="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png" />
  <gap:splash gap:platform="blackberry" src="res/screen/blackberry/screen-225.png" />
  <gap:splash gap:platform="ios" height="480" src="res/screen/ios/screen-iphone-portrait.png" width="320" />
  <gap:splash gap:platform="ios" height="960" src="res/screen/ios/screen-iphone-portrait-2x.png" width="640" />
  <gap:splash gap:platform="ios" height="1136" src="res/screen/ios/screen-iphone-portrait-568h-2x.png" width="640" />
  <gap:splash gap:platform="ios" height="1024" src="res/screen/ios/screen-ipad-portrait.png" width="768" />
  <gap:splash gap:platform="ios" height="768" src="res/screen/ios/screen-ipad-landscape.png" width="1024" />
  <gap:splash gap:platform="winphone" src="res/screen/windows-phone/screen-portrait.jpg" />
  <access origin="*" />

</widget>

这是我的index.html 文件:

<head>
    <title></title>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />        
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1" />

    <!--Styles-->

    <!--Javascript-->
    <script src="js/libs/jquery/jquery-1.9.1min.js" type="text/javascript" charset="utf-8"></script>
    <!--Phone Gap-->
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="phonegap.js"></script>

    <script>
        /* Set jQueryMobile default behavior */
        $(document).bind("mobileinit", function () {
            $.mobile.defaultPageTransition = "slide";
        });

        function onError(errorText) {  //default error behavior
            globalVarStore.error.message = errorText;
            $.mobile.loading('hide');
            $.mobile.changePage("error.html", { role: "dialog" });
        }
    </script>

    <!--Jquery Mobile / Libs-->
    <script src="js/libs/jqmobile/jqmobile.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/libs/jquery.validate.js" type="text/javascript" charset="utf-8"></script>

    <!---Page Controllers-->


    <script>
            app.initialize();

        $(function () {
            //some jquery stuff here.                              
        });

    </script>        
</head>

这是我的 index.js 文件,其中包含 deviceReady 处理程序。这对于 iPhone 来说效果很好。

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler           
    onDeviceReady: function () {
    itDoesntMatterWhatIsHereItDoesntFire();
    },

最佳答案

在 header 中调用 app.initialize() 并不能保证页面已准备就绪。尝试将其添加到正文的 onload 属性中:

<body onload="app.initialize()">

关于javascript - phonegap/cordova onDeviceReady() 不触发 android 4.1.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24898755/

相关文章:

javascript - 从 Firebase 数据库检索过去 2 天的结果

android - 强制 Renderscript 在 CPU 或 GPU 上运行(至少出于性能调整目的)

android - 是否可以直接在string.xml中设置字符串的颜色?

java - Android httpGET JSON 数组

javascript - PhoneGap/Cordova sleep 时下载中断,如何防止

android - IsoDep APDU SELECT 返回 6FXX

javascript - querySelectorAll 不起作用?

javascript - $(this) 在 .append() 中不起作用

javascript - 窗口大小调整时 Canvas 重绘/删除

cordova - 如何使用 phonegap 选择手机铃声?