Wednesday 2 May 2012

Getting Device IMEI From Android to Phonegap

In Android :
************
import android.content.Context;
import android.telephony.TelephonyManager;

String IMEI;
int Phno;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
IMEI  = telephonyManager.getDeviceId();
Phno =  telephonyManager.getPhoneType();
super.loadUrl("file:///android_asset/www/index.html?IMEI="+IMEI+"&Phno="+Phno);

In Phonegap :
**************
var query = location.href.substring((location.href.indexOf('?')+1), location.href.length);
if(location.href.indexOf('?') < 0) query = '';
    querysplit = query.split('&');
    query = new Array();
for(var i = 0; i < querysplit.length; i++)
{
        var namevalue = querysplit[i].split('=');
        namevalue[1] = namevalue[1].replace(/\+/g, ' ');
        query[namevalue[0]] = unescape(namevalue[1]);
}

var DeviceIMEI = query['IMEI'];
alert("DeviceIMEI : " + DeviceIMEI);

4 comments:

  1. it didn't worked for me until I added the following lines before the definition of the class that extends DroidGap:

    import android.content.Context;
    import android.telephony.TelephonyManager;

    ReplyDelete