Thursday 31 October 2013

TelephonyManager Example

TelephonyManager Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.

Example:




Code:

activity_main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingTop="10dp" >
        
         <TextView
             android:id="@+id/textView1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="@string/imei"
             android:textAppearance="?android:attr/textAppearanceLarge"
             android:textSize="20sp"
             android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/subid"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/serialno"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/networkiso"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/simiso"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/softversion"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/voicemail"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/phonetype"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView16"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/roaming"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView19"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/networktype"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView20"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView21"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dataconnectionstate"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView23"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/dataconnectionactivity"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView24"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView25"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/celllocation"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView26"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView27"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/operatorname"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView28"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/line1number"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView30"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/clear"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/red" />
        
        
    </LinearLayout>

</ScrollView>


strings.xml



<?xml version="1.0" encoding="utf-8"?>

<resources>
<string name="app_name">TelephonyManager</string>
<string name="action_settings">Settings</string>
<color name="indigo">#4b0082</color>
<color name="blue">#0000ff</color>
<color name="white">#ffffff</color>
<color name="red">#ff0000</color>
<string name="imei">IMEI Number</string>
<string name="subid">Subscriber ID</string>
<string name="serialno">SIM Serial Number</string>
<string name="clear"></string>
<string name="networkiso">Network Country ISO Code</string>
<string name="simiso">SIM Country ISO Code</string>
<string name="softversion">Device Software Version</string>
<string name="voicemail">Voice Mail Number</string>
<string name="phonetype">Phone Type</string>
<string name="roaming">Roaming Status</string>
<string name="networktype">Network Type</string>
<string name="dataconnectionstate">Data Connection State</string>
<string name="dataconnectionactivity">Data Connection Activity</string>
<string name="celllocation">Your Cell Location</string>
<string name="operatorname">Network Operator Name</string>
<string name="line1number">Phone Line1 Number</string>
<string name="heading">MOBILE INFORMATION</string>
</resources>


MainActivity.java

package in.androiddevelopmentanddiscussion.telephonymanager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView imei,subid,serialno,Nisocode,Sisocode,softversion,vmailno,phtype,roam,nettype,dstate;
TextView dactivity,clocation,operatorname,numberlin1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imei = (TextView)findViewById(R.id.textView2);
subid = (TextView)findViewById(R.id.textView4);
serialno = (TextView)findViewById(R.id.textView6);
Nisocode = (TextView)findViewById(R.id.textView8);
Sisocode = (TextView)findViewById(R.id.textView10);
softversion = (TextView)findViewById(R.id.textView12);
vmailno = (TextView)findViewById(R.id.textView14);
phtype = (TextView)findViewById(R.id.textView16);
roam = (TextView)findViewById(R.id.textView18);
nettype = (TextView)findViewById(R.id.textView20);
dstate = (TextView)findViewById(R.id.textView22);
dactivity = (TextView)findViewById(R.id.textView24);
clocation = (TextView)findViewById(R.id.textView26);
operatorname = (TextView)findViewById(R.id.textView28);
numberlin1 = (TextView)findViewById(R.id.textView30);
TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)tm.getCellLocation();
String IMEINumber=tm.getDeviceId();
String subscriberID=tm.getSubscriberId();
String SIMSerialNumber=tm.getSimSerialNumber();
String networkCountryISO=tm.getNetworkCountryIso();
String SIMCountryISO=tm.getSimCountryIso();
String softwareVersion=tm.getDeviceSoftwareVersion();
String voiceMailNumber=tm.getVoiceMailNumber();
int phonetype = tm.getPhoneType();
boolean roaming = tm.isNetworkRoaming();
int networktype = tm.getNetworkType();
int datastate = tm.getDataState();
String simoperatorname = tm.getSimOperatorName();
String line1number = tm.getLine1Number();
int dataactivity = tm.getDataActivity();
int cellid = cellLocation.getCid();
    int locareacode = cellLocation.getLac();
 
imei.setText(IMEINumber);
subid.setText(subscriberID);
serialno.setText(SIMSerialNumber);
Nisocode.setText(networkCountryISO);
Sisocode.setText(SIMCountryISO);
softversion.setText(softwareVersion);
vmailno.setText(voiceMailNumber);
operatorname.setText(simoperatorname);
numberlin1.setText(line1number);
clocation.setText("Mobile Location: "+cellLocation.toString()+"\nGSM Cell Id: "+cellid+"\nGSM Location Area Code: "+locareacode);
 
 
 
switch(phonetype)
{
case TelephonyManager.PHONE_TYPE_GSM:
phtype.setText("GSM");
break;
case TelephonyManager.PHONE_TYPE_CDMA:
phtype.setText("CDMA");
break;
case TelephonyManager.PHONE_TYPE_NONE:
phtype.setText("Unknown");
default:
 
}
 
if(roaming==true)
{
roam.setText("You are in Roaming");
}
else
{
roam.setText("You are at your Home Network");
}
 
 
switch(networktype)
         {
           case TelephonyManager.NETWORK_TYPE_1xRTT:
        nettype.setText("CDMA - 2000");
        break;
           case TelephonyManager.NETWORK_TYPE_CDMA:
        nettype.setText("CDMA");
             break;
           case TelephonyManager.NETWORK_TYPE_EDGE:
        nettype.setText("GSM - EDGE");
             break;
           case TelephonyManager.NETWORK_TYPE_EVDO_0:
        nettype.setText("CDMA - EVDO A");
             break;
           case TelephonyManager.NETWORK_TYPE_EVDO_A:
          nettype.setText("CDMA - EVDO A");
             break;
           case TelephonyManager.NETWORK_TYPE_GPRS:
          nettype.setText("GSM - GPRS");
             break;
           case TelephonyManager.NETWORK_TYPE_UMTS:
          nettype.setText("UMTS");
             break;
           case TelephonyManager.NETWORK_TYPE_UNKNOWN:
           default:
          nettype.setText("Unknown");
         }
if(datastate == TelephonyManager.DATA_CONNECTED)
{
 dstate.setText("Data Connection Connected"); 
}
else
{
dstate.setText("Data Connection Not Connected"); 
}
 
switch(dataactivity)
{
case TelephonyManager.DATA_ACTIVITY_NONE:
dactivity.setText("No traffic");
break;
case TelephonyManager.DATA_ACTIVITY_IN:
dactivity.setText("Currently receiving IP PPP traffic");
break;  
case TelephonyManager.DATA_ACTIVITY_OUT:
dactivity.setText("Currently sending IP PPP traffic");
break;
 
case TelephonyManager.DATA_ACTIVITY_INOUT:
dactivity.setText("Currently both sending and receiving IP PPP traffic");
break;  
case TelephonyManager.DATA_ACTIVITY_DORMANT:
dactivity.setText("Data connection is active, but physical link is down");
 
}


}


}


Manifest

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


Download full project from here




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

AsyncTask Example

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.


Example:




Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Start" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.asyntask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView tv;
Button btn;
ProgressBar pb;
ProgressDialog dialog;
int i =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.textView1);
btn = (Button)findViewById(R.id.button1);
pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setMax(100);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new As().execute(i);
i++;
}
});

}

class As extends AsyncTask<Integer, Integer, Integer>
{
int i;
@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub

while(i<100)
{
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
i++;
publishProgress(i);

Log.i("MO",String.valueOf(i)+": "+String.valueOf(params[0]));


}
return null;

}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
tv.setText(String.valueOf(values[0]));
pb.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("Download in progress");
dialog.setMessage("Please wait....");
dialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
dialog.dismiss();
super.onPostExecute(result);
}



}

}

Download full project from here 




Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en