Showing posts with label Getting an Access of Android Java Code to a PhoneGap JavaScript.. Show all posts
Showing posts with label Getting an Access of Android Java Code to a PhoneGap JavaScript.. Show all posts

Tuesday, May 24, 2011

Getting an Access of Android Java Code to a PhoneGap JavaScript.

PhoneGap Provides a standard set of functions available in PhoneGap docs to access the internal Device Details like the list of the phone numbers, access to camera etc.

Apart from this standard functions, if our requirements extends then these available functions, then we need to create our own custom logical functions which access the internal device hardware details like 'imei' number etc or to apply some custom logic or validate the data and then make it available to PhoneGap, then this article helps to do the same.

Though PhoneGap provides the 'imei' number through the standard function 'device.uuid' but in Android 2.1, it is still an issue. For such cases we need to take the help of the Native Android Java Code and then make it accessible to PhoneGap, here is how we will be doing the same.

Following example is of getting the device's 'imei' number through the Android's TelephonyManager class and then make it available to PhoneGap JavaScript:

Create a custom Class as follows:

import com.phonegap.DroidGap;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.webkit.WebView;


public class CustomNativeAccess {
    private WebView mAppView;
    private DroidGap mGap;
    public CustomNativeAccess(DroidGap gap, WebView view)
    {
        mAppView = view;
        mGap = gap;
    }   
    public String getImeiNumber(){
    TelephonyManager tm = (TelephonyManager) mGap.getSystemService(Context.TELEPHONY_SERVICE);
        String imeiId = tm.getDeviceId();      
        return imeiId;
    }
}

Function 'getImeiNumber()' retuns the 'imei' number of the device and the same function we will be using in PhoneGap Javascript.

Now Comming to the main Activity Class of the Application:


import com.phonegap.*;
public class MyActivity extends DroidGap {
    /** Called when the activity is first created. */
CustomNativeAccess cna;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        super.init();    
        cna = new CustomNativeAccess(this, appView);    
        appView.addJavascriptInterface(cna, "CustomNativeAccess");      
        super.loadUrl("file:///android_asset/www/index.html");
    }
}

'appView' is an built in variable of DroidGap and it is initialized through the call 'super.init();'.
'appView.addJavascriptInterface' adds all the function of the specified class i.e. 'CustomNativeAccess' here in example, and makes it accessible in the JavaScript in PhoneGap Application.

Finally, to get the access in JavaScript,


var imeiNumber = window.CustomNativeAccess.getImeiNumber();

returns the valid 'imei' number.

I hope this article is useful for getting the Native Android Java Code Access to the PhoneGap's JavaScript.

~ Mayuri




Best Car pool Android Application Best Car Pool Application Source Code Download Here



Best Social Network Cordova Ionic Application Best Social Networking Source Code Cordova Ioinc Application Download Here


Best Android Application, Android Restaurant System Best Android Restaurant Application Source code download here


Best Android Application Android Chat Source code download Android Chat Source code download


Best Android Quiz Source Code Download Best Android Quiz Source Code Download here

More and Lots of useful Android source codes here Best Android Source codes download here


Multi-Agent Orchestration in Vibe Coding: How I Built Chai & Chapters with Google Antigravity

If you have been following my tech journey on Catch Mayuri, you know how passionate I am about exploring the absolute cutting edge of web de...