0

The method onDeviceRead() don´t fire when I am running it in an Crosswalk-Cordova-Project.

I have tested the following versions of Crosswalk Cordova Android(ARM):

-11.40.277.7
-12.41.296.4
-13.41.313.0

I am using cordova 3.6.3

Here is my Code from the HTML-File:

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />                         
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Test</title>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
    <script type="text/javascript" charset="utf-8">

    function onLoad() {

            document.addEventListener("deviceready", onDeviceReady, false);
        }

        // device APIs are available
        //
        function onDeviceReady() {
            alert("123");
        }            

    </script>

</head>
<body onload="onLoad()">
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>

</body>

Here is the documentation from cordova: http://docs.phonegap.com/en/edge/cordova_events_events.md.html#deviceready

Thanks in advance!

WhiteBird
  • 33
  • 1
  • 6
  • are you able to see 'Apache cordova. Device is Ready` on the Mobile screen? Try to comment the `app.initialize()` call and check if it alerts `123` on the screen. Also check if `cordova.js` is available in `your_project\platforms\android\assets\www` directory – frank Apr 10 '15 at 07:01
  • Yes, i can see "Apache cordova. Device is Ready" on the mobile screen(Do you mean the content of the
    - tag? ). I commented the app.initialize() call and nothing happend. Still can´t see the the alert. The cordova.js is available in the directory. I already replaced the cordova.js with a newer version of cordova. Nothing...
    – WhiteBird Apr 10 '15 at 07:41

1 Answers1

0

If you can see Apache cordova. Device is Ready which means that the cordova api has been initialized. The Apache cordova. Device is Ready is set by the app.initialize() call which is located in the index.js file.

To make it simpler. just comment the app.initialize() call. Remove the onload attribute in the body tag and replace the following code in the script tag.

<script type="text/javascript" charset="utf-8">

  document.addEventListener("deviceready", onDeviceReady, false);

  // device APIs are available
  //
  function onDeviceReady() {
        alert("123");
  }            

</script>

You should see an alert on your screen.

frank
  • 3,180
  • 3
  • 16
  • 18
  • still can´t see the alert :-/ Where exactly should i see "apache cordova. Device is Ready" – WhiteBird Apr 10 '15 at 08:47
  • you said in your earlier comment that you can see "apache cordova. Device is Ready" in your mobile screen.? How did you get to that point in your program? – frank Apr 10 '15 at 10:07
  • I meant that I can see the content of the HTML-File. In The HTML-File is mentioned

    Apache Cordova

    – WhiteBird Apr 10 '15 at 11:02
  • If start the html-file in chrome i get a few failures. Here it is: Uncaught SyntaxError: Unexpected token ILLEGAL index.js:1 Uncaught SyntaxError: Unexpected token ILLEGAL index.html:122 Uncaught ReferenceError: app is not defined – WhiteBird Apr 10 '15 at 12:33
  • So the cordova.js and the index.js won´t execute when running the index.html in the windows chrome browser. Is that normal? – WhiteBird Apr 10 '15 at 12:51
  • The cordova.js won't run in the windows chrome browser. Firstly because you might be using the `file://` protocol (i.e loading from the local file system) which is not allowed for security reasons. secondly the cordova.js file is dependent on java libraries that act as a bridget between the Webview JavaScript and the Native code. Can you try to recreate your project afresh and check whether the `deviceready` event is fired.? I am still not clear why you are not able to get the cordova stuff working. – frank Apr 10 '15 at 13:22
  • can you gives us the steps as to how is your cordova project set up? what commands are you running to setup/create your cordova project? – frank Apr 10 '15 at 13:25
  • Isn´t it possible to debbug the app with this tutorial? http://gonzalo123.com/2014/08/04/debugging-android-cordovaphonegap-apps-with-chrome/ – WhiteBird Apr 10 '15 at 13:39
  • 1. create the HTML5-Cordova Project with Crosswalk: /path/to/crosswalk-cordova-android/bin/create HelloWorld com.example.helloworld HelloWorld 2. add the – WhiteBird Apr 10 '15 at 13:49
  • Don´t know why but now it works! Thank you for your patience ;) – WhiteBird Apr 10 '15 at 13:53
  • did you create a new project or your old project started working. – frank Apr 10 '15 at 13:54
  • a new one. Even the css won´t execute in the old ones. Now i see the correct start screen of cordova. Very strange... – WhiteBird Apr 10 '15 at 13:57
  • Sometimes Crosswalk creates a HTML-Project where the index.html or the css files have a different Encoding. That was the problem. – WhiteBird Apr 15 '15 at 06:48