Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap android - deviceready event not fired

I had searched the web for solutions to my problem and found many related threads, unfortunately, none of the solutions provided works for my case. I am new to android development and followed the documentation found at Apache Cordova! for configuration.

Problem: The deviceready event is not fired, below are my codes and configuration together with error log. Please advise me on what i had done wrong, thanks!!!

Emulator

AVD Device: nexus 7
Target: Android 4.2
Memory RAM: 512

MainActivity.java

    package com.example.pollo;

    import android.os.Bundle;
    import org.apache.cordova.*;
    import android.app.Activity;

    public class MainActivity extends DroidGap {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.loadUrl("file:///android_asset/www/index.html");
        }
    }

index.html

    <html>
      <head>
        <title></title>
        <script src="cordova-2.2.0.js"></script>
        <script>
                function onLoad(){
                   console.log("Now Loading");
                   document.addEventListener("deviceready", myDeviceReadyListener, false);  
                }


                function myDeviceReadyListener(){
                    console.log("Device ready");
                    navigator.notification.alert("Phone is ready!");

                    var myDiv = document.getElementById('props');
                    myDiv.innerHTML = 'Device Name: ' + device.name;
                }
            </script>
      </head>
      <body onload="onLoad()">
        <h1>X App V.11</h1>
        <p id="props">Loading device properties...</p>
      </body>
    </html>

AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.pollo"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="17" />

        <supports-screens
            android:largeScreens="true"
            android:normalScreens="true"
            android:smallScreens="true"
            android:xlargeScreens="true"
            android:resizeable="true"
            android:anyDensity="true"
            />    
        <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
        android:name="com.example.pollo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

    </manifest>

Config

assets>www>cordova-2.2.0.js
assets>www>index.html
res>xml>config.xml
AndroidManifest.xml
libs>cordova-2.2.0.jar
libs>android-support-v13.jar

Logcat - Error Log

 12-31 01:50:58.323: E/Trace(659): error opening trace file: No such file or directory (2)
 12-31 01:51:45.653: E/StrictMode(552): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d1b278 that was originally bound here

The onLoad() is called but the myDeviceReadyListener() is not called~ Does it got anything to do with the emulator that i chose?

like image 762
Esoup Edwin Ang Avatar asked Dec 31 '12 03:12

Esoup Edwin Ang


1 Answers

Funny, just faced the same problem yesterday. I spent hours debugging and all I could find is that at line 6016 in cordova-2.2.0.js, the app seems to silently quit executing. Why? No idea... there are no errors in the logs. Perhaps some new permissions or something along those lines.

Anyway, after wasting many hours debugging, I started from scratch using the PhoneGap tutorial. I noticed that at the time when PhoneGap 2.2.0 was released, they had used API level 16, not 17 (which is what I was using and what you are using). As soon as I set the app's target to API Level 16 everything started working again.

Takeaway: set your app's target API level to 16 (Android 4.1.2) for now and your app will receive the deviceready event.

like image 70
Theo Avatar answered Oct 11 '22 20:10

Theo