Telephonymanager Application
The main function of a mobile phone is to call the phone. Without a SIM card provided by a telecommunications company, the phone cannot be called normally. So, how can we obtain the SIM card status and related information?
The telephonymanager (Android. telephony. telephonymanager) object in the android API provides several methods to quickly obtain the SIM card status and related information.
ProgramUse getsystemservice (telephony_service) to obtain the telephonymanager object, use the method provided by telephonymanager to obtain the SIM card status and related information, store the obtained information in the Custom myadapter, and finally use setlistadapter () display the information in myadpter in listview.
View code
1 Import Android. telephony. telephonymanager;
2
3
4
5 Public Class Ex05_18 extends listactivity {
6
7 Private Telephonymanager telmgr;
8
9 Private List <string> item = New Arraylist <string> ();
10
11 Private List <string> value =New Arraylist <string> ();
12
13
14
15 @ Suppresswarnings ( " Static-Access " )
16
17 @ Override
18
19 Public Void Oncreate (bundle savedinstancestate ){
20
21 Super. oncreate (savedinstancestate );
22
23 /* Load main. xml Layout */
24
25 Setcontentview (R. layout. Main );
26
27 Telmgr = (telephonymanager) getsystemservice (telephony_service );
28
29 /* Write the obtained information to the list. */
30
31 /* Get SIM card status */
32
33 Item. Add (getresources (). gettext (R. String . Str_list0). tostring ());
34
35 If (Telmgr. getsimstate () = telmgr. sim_state_ready ){
36
37 Value. Add ( " Good " );
38
39 } Else If (Telmgr. getsimstate () = telmgr. sim_state_absent ){
40
41 Value. Add ( " No SIM card " );
42
43 } Else {
44
45 Value. Add ( " SIM card locked or unknown " );
46
47 }
48
49 /* Get SIM card number */
50
51 Item. Add (getresources (). gettext (R. String . Str_list1). tostring ());
52
53 If (Telmgr. getsimserialnumber ()! =Null ){
54
55 Value. Add (telmgr. getsimserialnumber ());
56
57 } Else {
58
59 Value. Add ( " Unable to obtain " );
60
61 }
62
63 /* SIM card supplier acquisitionCode */
64
65 Item. Add (getresources (). gettext (R. String . Str_list2). tostring ());
66
67 If (Telmgr. getsimoperator (). Equals ("" )){
68
69 Value. Add ( " Unable to obtain " );
70
71 } Else {
72
73 Value. Add (telmgr. getsimoperator ());
74
75 }
76
77 /* Get SIM card Supplier name */
78
79 Item. Add (getresources (). gettext (R. String . Str_list3). tostring ());
80
81 If (Telmgr. getsimoperatorname (). Equals ( "" )){
82
83 Value. Add ( " Unable to obtain " );
84
85 } Else {
86
87 Value. Add (telmgr. getsimoperatorname ());
88
89 }
90
91 /* Country of the obtained SIM card */
92
93 Item. Add (getresources (). gettext (R. String . Str_list4). tostring ());
94
95 If (Telmgr. getsimcountryiso (). Equals ( "" )){
96
97 Value. Add ( " Unable to obtain " );
98
99 } Else {
100
101 Value. Add (telmgr. getsimcountryiso ());
102
103 }
104
105 /* Use a custom myadapter to pass data to listactivity */
106
107 Setlistadapter ( New Myadapter ( This , Item, value ));
108
109 }
110
111
112
113 }