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.
~ Seshadri Pera
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.
~ Seshadri Pera
Hi, I followed this example and it worked. However, when I build my app using Proguard the function no longer works. I outlined my probleme here: http://stackoverflow.com/questions/23229221/how-to-define-proguard-to-keep-java-interface-for-imei-using-phonegap-for-andro .Have you any ideas on how to use this with Proguard?
ReplyDeleteThanks.