Pages

Subscribe:

Thursday, March 31, 2011

Android - Sample Code to Get Phone Details


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/btnPhoneDetails"
android:layout_width="148px"
android:layout_height="wrap_content"
android:text="Get Phone Details"
android:layout_x="24px"
android:layout_y="26px"
>
</Button>
<TextView
android:id="@+id/myLabel"
android:layout_width="292px"
android:layout_height="132px"
android:text="TextView"
android:gravity="center"
android:layout_x="14px"
android:layout_y="88px"
>
</TextView>
<Button
android:id="@+id/btnDial"
android:layout_width="77px"
android:layout_height="wrap_content"
android:text="Dial"
android:layout_x="220px"
android:layout_y="294px"
>
</Button>
<EditText
android:id="@+id/txtPhoneNo"
android:layout_width="190px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="20px"
android:layout_y="293px"
>
</EditText>
<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number with STD Code"
android:layout_x="19px"
android:layout_y="266px"
>
</TextView>
</AbsoluteLayout>

Java Code

package com.myphone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PhoneDetails extends Activity {

Button btnDial;
Button btnPhoneInfo;

EditText txtPhoneNo;
TextView txtPhoneDetails;

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

btnDial = (Button) findViewById(R.id.btnDial);
btnPhoneInfo = (Button) findViewById(R.id.btnPhoneDetails);

txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
txtPhoneDetails = (TextView) findViewById(R.id.myLabel);

btnDial.setOnClickListener((OnClickListener) new dialNumber());
btnPhoneInfo.setOnClickListener((OnClickListener) new printDetails());
}

class dialNumber implements OnClickListener {

@Override
public void onClick(View v) {
dialThisNumber(txtPhoneNo.getText().toString());
}
}

class printDetails implements OnClickListener {

@Override
public void onClick(View v) {
printPhoneDetails();

}

}

private void printPhoneDetails() {
Log.i("PHONE_DETAILS", "Reading phone details");
String srvcName = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(srvcName);
int phoneType = telephonyManager.getPhoneType();
String strPhoneType;
Log.i("PHONE_DETAILS", "Verifying Phone Type..");
switch (phoneType) {
  case (TelephonyManager.PHONE_TYPE_CDMA): strPhoneType = "CDMA"; break;
  case (TelephonyManager.PHONE_TYPE_GSM) : strPhoneType = "GSM"; break;
  case (TelephonyManager.PHONE_TYPE_NONE): strPhoneType = "None"; break;
  default: strPhoneType = "Unbale to Find."; break;
}

// -- These require READ_PHONE_STATE uses-permission --
// Read the IMEI for GSM or MEID for CDMA
Log.i("PHONE_DETAILS", "Reading IMEI No");
String deviceId = telephonyManager.getDeviceId();
// Read the software version on the phone (note -- not the SDK version)
Log.i("PHONE_DETAILS", "Reading software details");
String softwareVersion = telephonyManager.getDeviceSoftwareVersion();
// Get the phone's number
Log.i("PHONE_DETAILS", "Reading phone number");
String phoneNumber = telephonyManager.getLine1Number();
// Network Details
// Get connected network country ISO code
String networkCountry = telephonyManager.getNetworkCountryIso();
// Get the connected network operator ID (MCC + MNC)
String networkOperatorId = telephonyManager.getNetworkOperator();
// Get the connected network operator name
String networkName = telephonyManager.getNetworkOperatorName();
// Get the type of network you are connected to
int networkType = telephonyManager.getNetworkType();
String nType;
switch (networkType) {
  case (TelephonyManager.NETWORK_TYPE_1xRTT)   : nType = "1xRTT"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_CDMA)    : nType = "CDMA";
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_EDGE)    : nType = "EDGE"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_EVDO_0)  : nType = "EVDO-0"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_EVDO_A)  : nType = "EVDO-A"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_GPRS)    : nType = "GPRS"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_HSDPA)   : nType = "HSDPA"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_HSPA)    : nType = "HSPA"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_HSUPA)   : nType = "HSUPA"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_UMTS)    : nType = "UMTS"; 
                                                 break;
  case (TelephonyManager.NETWORK_TYPE_UNKNOWN) : nType = "Unknown"; 
                                                 break;
  default: nType = "Unable to find."; break;
}
//Reading SIM details
int simState = telephonyManager.getSimState();
String simCountry = null;
String simOperatorCode = null;
String simOperatorName = null;
String simSerial = null;
switch (simState) {
  case (TelephonyManager.SIM_STATE_ABSENT): break;
  case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): break;
  case (TelephonyManager.SIM_STATE_PIN_REQUIRED): break;
  case (TelephonyManager.SIM_STATE_PUK_REQUIRED): break;
  case (TelephonyManager.SIM_STATE_UNKNOWN): break;
  case (TelephonyManager.SIM_STATE_READY): {
    // Get the SIM country ISO code
    simCountry = telephonyManager.getSimCountryIso();
    // Get the operator code of the active SIM (MCC + MNC)
    simOperatorCode = telephonyManager.getSimOperator(); 
    // Get the name of the SIM operator
    simOperatorName = telephonyManager.getSimOperatorName();
    // -- Requires READ_PHONE_STATE uses-permission --
    // Get the SIM's serial number
    simSerial = telephonyManager.getSimSerialNumber();
    break;
  }
  default: break;
}
  
Log.i("PHONE_DETAILS", "Merging phone details");
String pinfo = "Phone Type - " + strPhoneType + "\n";
pinfo = pinfo + "Devide ID - " + deviceId + "\n";
pinfo = pinfo + "Software Version - " + softwareVersion + "\n";
pinfo = pinfo + "Phone Number - " + phoneNumber + "\n";
pinfo = pinfo + "Network Type - " + nType + "\n";
pinfo = pinfo + "SIM Country - " + simCountry + "\n";
pinfo = pinfo + "SIM Operator Code - " + simOperatorCode + "\n";
pinfo = pinfo + "SIM Operator Name - " + simOperatorName + "\n";
pinfo = pinfo + "SIM Serial - " + simSerial + "\n";
Log.i("PHONE_DETAILS", "Showing phone details in label");
txtPhoneDetails.setText(pinfo);
}

private void dialThisNumber(String noToDial) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
+ noToDial));
startActivity(intent);
}
}

2 comments:

Unknown said...

Excellent information and thanks for you are tutorials.

Android Apps Development

From.Ae said...

Nice to read this article will be very helpful in the future, share more info with us. Good job!

Android App in Pakistan

Post a Comment