The 86th chapter, System service Telephony_service (learn Android from scratch)

Source: Internet
Author: User

The Telephonymanager class provides a series of get methods for accessing status and information related to mobile communication. These include mobile SIM status and information, the status of the telecommunications network, and the information of mobile phone users. You can use these get methods in your application to get related data.

Objects of the Telephonymanager class can be obtained by means of the Context.getsystemservice (Context.telephony_service) method, and it is important to note that the acquisition of some communication information has certain limitations on the permissions of the application. , you need to add the appropriate permissions for the development.

First, the design interface

1. layout file

Open the Res/layout/activity_main.xml file.
Enter the following code:

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <Button
  8. android:id="@+id/getphoneinfo"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Get mobile network Information" />
  12. </linearlayout>
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout     xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/getphoneinfo" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:text=" Get mobile network Information "/></linearlayout>


Ii. Procedure Documents

Open the "Src/com.genwoxue.contentprovider_b/mainactivity.java" file.
Then enter the following code:

[Java]View Plaincopyprint?
  1. Package com.genwoxue.telephony;
  2. Import Android.os.Bundle;
  3. Import Android.view.View;
  4. Import Android.view.View.OnClickListener;
  5. Import Android.widget.Button;
  6. Import Android.widget.EditText;
  7. Import Android.widget.Toast;
  8. Import android.app.Activity;
  9. Import Android.telephony.TelephonyManager;
  10. Import Android.content.Context;
  11. Public class Mainactivity extends Activity {
  12. private Button btnget=null;
  13. @Override
  14. protected void OnCreate (Bundle savedinstancestate) {
  15. super.oncreate (savedinstancestate);
  16. Setcontentview (R.layout.activity_main);
  17. btnget= (Button)Super.findviewbyid (r.id.getphoneinfo);
  18. Btnget.setonclicklistener (new Onclicklistener () {
  19. public void OnClick (View v)
  20. {
  21. StringBuilder info=New StringBuilder ();
  22. //Get Telephonymanager service
  23. Telephonymanager telphony= (Telephonymanager) mainactivity.  This.getsystemservice (Context.telephony_service);
  24. //Get the name of the mobile service provider
  25. Info.append (Telphony.getnetworkoperatorname () +"☆☆☆");
  26. //Get device number
  27. Info.append (Telphony.getdeviceid () +"☆☆☆");
  28. Toast.maketext (Getapplicationcontext (), info, Toast.length_long). Show ();
  29. }
  30. });
  31. }
  32. }
Package Com.genwoxue.telephony;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;import Android.app.activity;import Android.telephony.telephonymanager;import Android.content.context;public class Mainactivity extends Activity {private Button btnget=null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); btnget= (Button) Super.findviewbyid (r.id.getphoneinfo); Btnget.setonclicklistener (new Onclicklistener () {public void        OnClick (View v) {StringBuilder info=new StringBuilder (); Get Telephonymanager service Telephonymanager telphony= (Telephonymanager) MainActivity.this.getSystemService (        Context.telephony_service);        Get the name of the mobile provider Info.append (Telphony.getnetworkoperatorname () + "☆☆☆"); Get device number Info.append (Telphony.getdeviceid () + "☆☆☆");        Toast.maketext (Getapplicationcontext (), info, Toast.length_long). Show (); }        });}}

Iii. Configuration Files

Open the "androidmanifest.xml" file.

Then enter the following code:

[HTML]View Plaincopyprint?
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="Com.genwoxue.telephony"
  4. android:versioncode="1"
  5. android:versionname="1.0" >
  6. <uses-sdk
  7. android:minsdkversion="one"
  8. android:targetsdkversion=" /> "
  9. <uses-permission android:name="Android.permission.READ_PHONE_STATE"/>
  10. <application
  11. android:allowbackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/apptheme" >
  15. <activity
  16. android:name="com.genwoxue.telephony.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="Android.intent.action.MAIN" />
  20. <category android:name="Android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </Activity>
  23. </Application>
  24. </manifest>  
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.genwoxue.telephony "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "one" android:targetsdkversion= "/> <uses-permission android:name=" Android Oid.permission.READ_PHONE_STATE "/> <application android:allowbackup=" true "android:icon=" @drawable/            Ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "com.genwoxue.telephony.MainActivity" android:label= "@string/app_name" > <i ntent-filter> <action android:name= "Android.intent.action.MAIN"/> <category and Roid:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> </appl Ication></manifest>

Note: You need to add permissions to the Androidmanifest.xml file:

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

Iv. Results of operation

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.