Thursday, December 4, 2014

Get PhoneNumber and mobile IMEI Number from Android Device using Phonegap

I was a bit surprised when my friend asked me how to do this in one day. I find the solution for Get Phone imei number and PhoneNumber using Phonegap. Its just very easy to implement in phonegap android application.
Just follow this steps:
Step:1
Select your cordovaActivity file from the phonegap src folder.
copy this javascriptInterface and past in your cordovaActivity class onCreate method.

Change your Class name in javascriptInterface Parameter.

 appView.addJavascriptInterface(this, "CordovaApp");             
        Log.d(TAG, " after finish toast " );   


Step:2

This method is get imei number from the Android phone. just copy and past your cordovaActivity class inside.

 @JavascriptInterface
  public String get_imei() 
  {
  String imei="";
  try
  {
  TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     imei = telephonyManager.getDeviceId();
  }catch(Exception e)
  {
  Log.e(TAG, "Exception at onCreate()" + e.toString()); 
  }
    return imei;    
  }


Step:3

This method is get Phonenumber which you insert in your Android mobile. just copy and past your cordovaActivity class inside.

@JavascriptInterface
  public String getMobileNumber() 
  {
  String PhoneNumber="";
  try
  {
 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
      PhoneNumber = tm.getLine1Number();
  }catch(Exception e)
  {
  Log.e(TAG, "Exception at getMobileNumber()" + e.toString()); 
  }
 return PhoneNumber;    
  }


Step:4

Just add all the blow Packages in your cordovaActivity class.

import android.telephony.TelephonyManager;
import android.webkit.JavascriptInterface;
import android.widget.Toast;


Step:5

This one add in your androidmanifest.xml file.

<uses-permission 
    android:name="android.permission.READ_PHONE_STATE" />


Step:6

this is on javascript to get Phonenumber and imei number which you want to call this methods from the javascript.

var phone imei_number = window.CordovaApp.get_imei();
var phonenumber = window.CordovaApp.getMobileNumber();
This is on Final Code:
package com.example.timers;

import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;
import org.apache.cordova.*;

public class CordovaApp extends CordovaActivity
{
  
 private static final String TAG = "CordovaApp";
 
 CustomNativeAccess cna; 
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        appView.addJavascriptInterface(this, "CordovaApp");             
        Log.d(TAG, " after finish toast " );       
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);  
    }
 
 @JavascriptInterface
  public String get_imei() 
  {
  String imei="";
  try
  {
  TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     imei = telephonyManager.getDeviceId();
  }catch(Exception e)
  {
  Log.e(TAG, "Exception at onCreate()" + e.toString()); 
  }
    return imei;    
  }
 
 
 @JavascriptInterface
  public String getMobileNumber() 
  {
  String PhoneNumber="";
  try
  {
 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
      PhoneNumber = tm.getLine1Number();
  }catch(Exception e)
  {
  Log.e(TAG, "Exception at getMobileNumber()" + e.toString()); 
  }
 return PhoneNumber;    
  }
 
 
}

this code only for an Android.

1 comment: