Tips and Tricks in a world of Mix

Posts tagged ‘PhoneGap’

Call java object from javascript

More and more I see entire products built on this ability which is pretty simple really .

How can we connect a responsive and dynamic UI such as Html5 that is able to migrate easily between devices with the Java ability of controlling the device under Android OS ? 

The idea is to :

  1. Set the promise and enabling usage of javascript interface in Java
  2. Create the corresponding implementation for the javascript interface in Java 
  3. Call the Java class and functionality from javascript – letting you control the device with the whole power of Java drivers from html5 – JQuery UI .

How to recipe :

  • in App.java :   

private DefMemoryReleaser  dmr ;

   super.appView.getSettings().setJavaScriptEnabled(true);

   dmr = new DefMemoryReleaser(this, appView);

   appView.addJavascriptInterface(dmr, “JavaDefMemoryReleaser”);

 

  •  buildjava corresponding class DefMemoryReleaser

package com.EggedDefinition;

import org.apache.cordova.DroidGap;

import android.webkit.WebView;

public class DefMemoryReleaser {

         private WebView mAppView;

         private DroidGap mGap;

          public DefMemoryReleaser(DroidGap gap, WebView view)

         {

           mAppView = view;

           mGap = gap;

         }

         public void freeMemory(){

                System.runFinalization();

               Runtime.getRuntime().gc();

               System.gc();

         }

       }

 

  • in javascript : 

if (typeof (window.JavaDefMemoryReleaser) != ‘undefined’)

       window.JavaDefMemoryReleaser.freeMemory();

 

Now you are ready and set to build and sell security products for Android – Godspeed !  🙂

Debugging in Eclipse console.log app messages

 

For you to be able to see the console you’ve been writing in your app :

 

Window->Show View-> other –> write Logcat –>

You’ll get a console window listening to every message from your device.

image

edit filter –> Web Console

 

image

 

Now you’ll see your app messages

image

Using Eclipse with Phonegap for VS projects – Solution

Let’s assume that we’ve installed all the needed software as described in :

http://wiki.phonegap.com/w/page/16494774/Getting%20started%20with%20Android%20PhoneGap%20in%20Eclipse

 

Now we open the Eclipse :

image

File –> New –> Android Project

now will add the project definitions

image

When you choose Android 2.2 automatically Min SDK Version defined as 8

Package should be with two parts – package name and project name

Next –> Finish

 

Open new folders libs and assets\www

image

 

 

Go to phonegap source  and get the phonegap.0.9.6 jar and js files.

image

jar goes to libs

js goes to assets\www

image

 

Now we’ll add the phonegap library to the project so:

Right click on jar file –> build path –> add to build path

image

Afterwards you can see the referenced library added to the solution :

 

image

Now we can reference it in code .

Go to src –> Pack.MyTest –> MyTestActivity.java –>

Add import and change extending :

Original Source:

package Pack.MyTest;

import android.app.Activity;
import android.os.Bundle;

public class MyTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

 

Changed adding phonegap and extending from Droid and add the start page :

package Pack.MyTest;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.*;
import com.phonegap.*;

public class MyTestActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        ///add the start page
        super.loadUrl("file:///android_asset/www/index.html");
        setContentView(R.layout.main);
    }
}

Add to assets\www index.html start page as promissed :

image

(put the reference to phonegap.js inside)

 

Define the properties of the runner AVD :

image

image

Define what project you want to run :

image

Go to Right click MyTest –> Run as –> Android Application

 

image

 

Our Android application :

image

Go to your VS 2010 and open WebSite

image

 

Find your workspace for eclipse –> Open :

image

Work with VS – > JQM

image

Getting Started with PhoneGap–problems and solutions–Windows 7 64 bit

  • Well , first of all I’ve got problem activating Eclipse – fixed it as explained in a previous post .
  • I’ve discovered that even though you download the PhoneGap from their site or even from gitHub (even a forked one) , you still will have missing files –specifically the bin folder of the PhoneGap will stay empty . You can download it from my fork – here one by one (idiotic and sisyphean,but didn’t find other way ) .
  • When you’ve downloaded it don’t try to run it through the command line – it won’t work . Everyone is talking about the git Bash . Went through this tutorial on installing Git on Windows.

Didn’t have the option for installation.

Just entered the PortableGit-1.7.6-preview20110709 folder and run git-bash.bat

Then do the following :

 

image

    Tzvi@TZVI-PC /usr
    $ cd c:

    Tzvi@TZVI-PC /c
    $ cd NetProjects\Android\phonegap-0.9.6\phonegap-0.9.6\Android
    bash: cd: NetProjectsAndroidphonegap-0.9.6phonegap-0.9.6Android: No such file o
    directory

    The slush must be forward slush – “/” , so :

    Tzvi@TZVI-PC /c
    $ cd NetProjects/

    Tzvi@TZVI-PC /c/NetProjects
    $ cd Android/phonegap
    phonegap-0.9.6/      phonegap-0.9.6.zip   phonegap_workspaces/

    Tzvi@TZVI-PC /c/NetProjects
    $ cd Android/phonegap
    phonegap-0.9.6/      phonegap-0.9.6.zip   phonegap_workspaces/

    Tzvi@TZVI-PC /c/NetProjects
    $ cd Android/phonegap-0.9.6

    Tzvi@TZVI-PC /c/NetProjects/Android/phonegap-0.9.6
    $ cd phonegap-0.9.6/

    Tzvi@TZVI-PC /c/NetProjects/Android/phonegap-0.9.6/phonegap-0.9.6
    $ cd Android/

    Tzvi@TZVI-PC /c/NetProjects/Android/phonegap-0.9.6/phonegap-0.9.6/Android
    $ ruby "D:\MyDocuments\MyInstallers\Jquery\phonegap-1.0.0rc1\phonegap-1.0.0rc1\
    Android\Sample\bin\droidgap" "C:\NetProjects\Android\android-sdk\tools" "namete
    st" "packagenameTest"  "C:\NetProjects\WebSite1" "C:\NetProjects\Android\phoneg
    ap_workspaces\WebSite1"

    Tzvi@TZVI-PC /c/NetProjects/Android/phonegap-0.9.6/phonegap-0.9.6/Android
    $

also Go to  this tutorial – very helpful

 

 

Tried to run some empty application from Visual Studio into eclipse .

Failed on jquery javascript files.

 

So I went for the Demo they give you in the eclipse .

Made a workspace  with DemoProject . Got it right – without the js errors .

 

Now , I’d like to see what did I get .

So , apparently to receive the emulator you need to do this :image

 

 

Got stuck and got this:

image

Tag Cloud

%d bloggers like this: