Android TelephonyManager Details?

The android.telephony.TelephonyManager (TELEPHONYMANAGER) class provides information about the telephony services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.

Let's see the simple example of TelephonyManager that prints information of the telephony services As for new Eclipse up date like up date(Add new library project to Main Application, Mainxml and as well as fragementxml file).

activity_main.xml


  1.  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.telephonemanager.MainActivity"
        tools:ignore="MergeRootFrame" />

fragment_main.xml

  1.   <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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.telephonemanager.MainActivity$PlaceholderFragment" >

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

    </RelativeLayout>
MainActivity.java

  1.   public class MainActivity extends ActionBarActivity {
    private static TelephonyManager  tm;
    private static String IMEINumber, subscriberID,SIMSerialNumber,networkCountryISO,
    SIMCountryISO,softwareVersion,voiceMailNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get the instance of TelephonyManager  
    tm=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
            
    //Calling the methods of TelephonyManager the returns the information  
    IMEINumber=tm.getDeviceId();  
            subscriberID=tm.getDeviceId();  
            SIMSerialNumber=tm.getSimSerialNumber();  
            networkCountryISO=tm.getNetworkCountryIso();  
            SIMCountryISO=tm.getSimCountryIso();  
            softwareVersion=tm.getDeviceSoftwareVersion();  
            voiceMailNumber=tm.getVoiceMailNumber();  
            
            if (savedInstanceState == null) {
             getSupportFragmentManager().beginTransaction()
             .add(R.id.container, new PlaceholderFragment()).commit();
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
    return true;
    }
    return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    private TextView textView1;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    textView1=(TextView) rootView.findViewById(R.id.textview);  
    //textView1.setText("new data of this");
    //Get the phone type  
            String strphoneType="";             
            int phoneType=tm.getPhoneType();  
      
            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;  
            }    
          //getting information if phone is in roaming  
            boolean isRoaming=tm.isNetworkRoaming();  
              
            String info="Phone Details:\n";  
            info+="\n IMEI Number:"+IMEINumber;  
            info+="\n SubscriberID:"+subscriberID;  
            info+="\n Sim Serial Number:"+SIMSerialNumber;  
            info+="\n Network Country ISO:"+networkCountryISO;  
            info+="\n SIM Country ISO:"+SIMCountryISO;  
            info+="\n Software Version:"+softwareVersion;  
            info+="\n Voice Mail Number:"+voiceMailNumber;  
            info+="\n Phone Network Type:"+strphoneType;  
            info+="\n In Roaming? :"+isRoaming;  
            //displaying the information in the textView 
            textView1.setText(info); 
            
            return rootView;
    }
    }
    }
 AndroidManifest.xml

You need to give READ_PHONE_STATE permission in the AndroidManifest.xml file.

Output





No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...