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);
}
}

Android - Listview Sample


How to use Listview in Android?


Following sample code explains the use of listview in Android.


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ListView android:id="@+id/myList" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</ListView>
</LinearLayout>


Java Code


package com.list.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewSample extends Activity {
private enum myPhones {Android, iPhone, BlacBerry};
private ListView lv;
private String myArray[] = {"Android", "iPhone", "Blackberry"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        lv = (ListView) findViewById(R.id.myList);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myArray));
        lv.setOnItemClickListener((OnItemClickListener) new clicker());
        
    }
    
    class clicker implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String msg = null;
if(arg2 == myPhones.Android.ordinal()) {
msg = "You selected ";
} else if (arg2 == myPhones.iPhone.ordinal()) {
msg = "You selected ";
} else if (arg2 == myPhones.BlacBerry.ordinal()) {
msg = "You selected ";
}
Toast.makeText(getBaseContext(), msg + myArray[arg2], Toast.LENGTH_LONG).show();
}
    
    }
}

Android - Slider Sample Code


Screen


Layout

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TextView
android:id="@+id/widget35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"
android:layout_x="42px"
android:layout_y="30px"></TextView>

<SeekBar android:id="@+id/SeekBar1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:max="255"
android:minWidth="250px"
android:layout_x="42px"
android:layout_y="55px">
</SeekBar>
<TextView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"
android:layout_x="42px"
android:layout_y="130px"></TextView>

<SeekBar android:id="@+id/SeekBar2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:max="255"
android:minWidth="250px"
android:layout_x="42px"
android:layout_y="155px">
</SeekBar>
<TextView
android:id="@+id/widget37"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"
android:layout_x="42px"
android:layout_y="230px"></TextView>

<SeekBar android:id="@+id/SeekBar3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:max="255"
android:minWidth="250px"
android:layout_x="42px"
android:layout_y="255px">
</SeekBar>
<TextView
android:id="@+id/txtMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enrichware"
android:layout_x="42px"
android:layout_y="330px"></TextView>
</AbsoluteLayout>

Java Code

package com.color;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class ChangeColor extends Activity {

SeekBar redBar;
SeekBar greenBar;
SeekBar blueBar;

TextView msg;

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

msg = (TextView) findViewById(R.id.txtMessage);

redBar = (SeekBar) findViewById(R.id.SeekBar1);
greenBar = (SeekBar) findViewById(R.id.SeekBar2);
blueBar = (SeekBar) findViewById(R.id.SeekBar3);

redBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) new redClicker());
greenBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) new greenClicker());
blueBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) new blueClicker());
}

class redClicker implements SeekBar.OnSeekBarChangeListener {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
msg.setTextColor(Color.rgb(redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
}
class greenClicker implements SeekBar.OnSeekBarChangeListener {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
msg.setTextColor(Color.rgb(redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
}
class blueClicker implements SeekBar.OnSeekBarChangeListener {

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
try {
msg.setTextColor(Color.rgb(redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
}
}