Get phone information using button click event

By

October 15, 2012Android2 Comments

This tutorial is the part of Android Development series

1.What is Android ?
2.Hello world mobile application using android.
3.What is Android activity ?
4.Understanding Android layouts
5.Introduction to AndroidManifest file
6.Android programming using Intents
7.Get phone information using button click event

In this  tutorial we will setup an android application to learn the button click event handling.Coding in eclipse IDE is very user friendly for the newbies because of the automatic code completion.The eclipse editor will automatically implement the default code for the button events.

Task: Get the phone’s IMEI number , Sim card number and mobile operator name when a button is pressed.

Create a new project in eclipse and select the appropriate SDK versions of your choice.If you are not sure of how to create a new project you can refer to our previous tutorial.

Create the Layout

Design the page layout by placing the textviews and buttons.

android_button_click_event

activity_main,xml

 

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <textview android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="51dp"
        android:text="@string/label_id"
        tools:context=".MainActivity"></textview>




    <textview android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_marginLeft="43dp"
        android:layout_toRightOf="@+id/textView2"
        android:text=""></textview>




    <button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="114dp"
        android:text="Load Data"></button>




    <textview android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="26dp"
        android:text="Sim Number :"></textview>




    <textview android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/textView1"
        android:text=""></textview>




    <textview android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="31dp"
        android:text="Operator Name :"></textview>




    <textview android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_alignLeft="@+id/textView4"
        android:text=""></textview>
</relativelayout>

When the loaddata button is pressed our code logic will fetch the device id and other information and updates the textviews.

Now your UI is ready, next we will go ahead and code our logic.

Mainactivity.Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.example.device_info;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

&nbsp;

This is the default code that will be available by default when you create the project.We need to add the button control and assign it to the button id which we just created.This will be done by findViewById function.
 

OnClickListener

Type b.setOnClickListener(new View.OnClickListener() and select the dropdown to autocomplete the On-Click event listener function

1
2
3
4
5
6
7
8
b.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               
            }
        });

Whatever you write inside the onClick function will be executed when the button is pressed.

 

How to get Phone information ?

First of all we need to declare the permission to access the phone data on the android manifest file with the tag.

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

 

Telephony Manager

This is the class that provides the telephony information like device id,mobile operator name and subscriber data.

1
TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

 

readphone state example

Final output

When the button is pressed the telephony manager class is used to get device information and populated in text view using the setText() function.

Complete Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.example.device_info;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button b;
        final TextView devid;
        final TextView sim,os;
        setContentView(R.layout.activity_main);
       
        b=(Button)findViewById(R.id.button1);
        devid=(TextView)findViewById(R.id.textView1);
        sim=(TextView)findViewById(R.id.textView4);
        os=(TextView)findViewById(R.id.textView6);
        b.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
               
                String android_id =tm.getDeviceId().toString();
                devid.setText(android_id);
                String simnumber =tm.getLine1Number().toString();
                sim.setText(simnumber);
                String version=tm.getSimOperatorName().toString();
                os.setText(version);
                ;
            }
        });
       
   
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Do you think this Android Development tutorial is useful ?? If Yes, Share/Comment below.You can also Get Regular Updates. Subscribe to Free RSS Feeds or Email Updates. Follow us on Twitter @Devlup and Like us on Facebook.

2 Responses to “Get phone information using button click event”
  1. hari babu naidu daddala

    Its nice,but when I run this application in original device.I am getting only device name,I am not getting sim number and operator name,please suggest how to over come this problem.

    • Jeyaganesh

      Hari,
      Have you verified the mainifest file? android.permission.READ_PHONE_STATE should be included.

Leave a Reply

*