Android中TelephonyManager类的用法案例详解

时间:2021-11-30 06:19:39

本文以案例形式分析了android中telephonymanager类的用法。分享给大家供大家参考。具体如下:

目录结构:

Android中TelephonyManager类的用法案例详解

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <scrollview android:fillviewport="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <listview android:id="@+id/listview"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" />
  </scrollview>
</linearlayout>

array.xml文件:

?
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
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="listitem">     
    <item>设备编号</item>
    <item>sim卡国别</item>
    <item>sim卡序列号</item>
    <item>sim卡状态</item>
    <item>软件版本</item>
    <item>网络运营商代号</item>
    <item>网络运营商名称</item>
    <item>手机制式</item>
    <item>设备当前位置</item>
  </string-array>
  <string-array name="simstate">     
    <item>状态未知</item>
    <item>无sim卡</item>
    <item>被pin加锁</item>
    <item>被puk加锁</item>
    <item>被network pin加锁</item>
    <item>已准备好</item>
  </string-array>
  <string-array name="phonetype">     
    <item>未知</item>
    <item>gsm</item>
    <item>cdma</item>
  </string-array> 
</resources>

清单文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ljq.activity" android:versioncode="1"
  android:versionname="1.0">
  <application android:icon="@drawable/icon"
    android:label="@string/app_name">
    <activity android:name=".telephonymanageractivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category
          android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minsdkversion="7" />
  <uses-permission android:name="android.permission.access_coarse_location" />
  <uses-permission android:name="android.permission.read_phone_state" />
</manifest>

telephonymanageractivity类:

?
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
package com.ljq.activity;
import java.util.arraylist;
import android.app.activity;
import android.content.context;
import android.os.bundle;
import android.telephony.telephonymanager;
import android.view.gravity;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.textview;
public class telephonymanageractivity extends activity {
  private listview listview=null;
  private telephonymanager tm=null;
  private string[] phonetype=null;
  private string[] simstate=null;
  private string[] listitems=null;
  arraylist<string> listvalues=new arraylist<string>();
  baseadapter adapter=new baseadapter(){
    public int getcount() {
      return listitems.length;
    }
    public object getitem(int position) {
      return listitems[position];
    }
    public long getitemid(int position) {
      return position;
    }
    public view getview(int position, view convertview, viewgroup parent) {
      linearlayout ll=new linearlayout(telephonymanageractivity.this);
      ll.setorientation(linearlayout.vertical);
      textview tvitem=new textview(telephonymanageractivity.this);
      tvitem.settextsize(24);
      tvitem.settext(listitems[position]);
      tvitem.setgravity(gravity.left);//设置在父容器中的对齐方式
      ll.addview(tvitem);
      textview tvvalue=new textview(telephonymanageractivity.this);
      tvvalue.settextsize(18);          //设置字体大小
      tvvalue.settext(listvalues.get(position));  //设置显示的内容
      tvvalue.setpadding(0, 0, 10, 10);      //设置四周边界
      tvvalue.setgravity(gravity.right); 
      ll.addview(tvvalue);
      return ll;
    }
  };
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    listitems=getresources().getstringarray(r.array.listitem);
    simstate=getresources().getstringarray(r.array.simstate);
    phonetype=getresources().getstringarray(r.array.phonetype);
    tm=(telephonymanager)getsystemservice(context.telephony_service);
    listview=(listview)findviewbyid(r.id.listview);
    initlistvalues();
    listview.setadapter(adapter);
  }
  /**
   * 获取各个数据项的值
   */
  public void initlistvalues(){     
    listvalues.add(tm.getdeviceid());//获取设备编号
    listvalues.add(tm.getsimcountryiso());//获取sim卡国别
    listvalues.add(tm.getsimserialnumber());//获取sim卡序列号 
    listvalues.add(simstate[tm.getsimstate()]);//获取sim卡状态
    listvalues.add((tm.getdevicesoftwareversion()!=null?tm.getdevicesoftwareversion():"未知"));  //获取软件版本
    listvalues.add(tm.getnetworkoperator());//获取网络运营商代号
    listvalues.add(tm.getnetworkoperatorname());//获取网络运营商名称
    listvalues.add(phonetype[tm.getphonetype()]);//获取手机制式
    listvalues.add(tm.getcelllocation().tostring());//获取设备当前位置
  }
}

运行结果:

 Android中TelephonyManager类的用法案例详解

希望本文所述对大家的android程序设计有所帮助。