当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

10.1 TelephonyManager(电话管理器)

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:27:54

本节引言:本章节是Android基础入门教程的最后一章,主要讲解是一些零零散散的一些知识点,以及一些遗漏知识点的补充,这些零散的知识点包括,各种系统服务的使用,比如本节的电话管理器,短信管理器,振动器,闹钟,壁纸等等,还有传感器之类的东西!乱七八糟什么都有哈!好的,本节我们要学习的是TelephonyManager,见名知义:用于管理手机通话状态,获取电话信息(设备信息、sim卡信息以及网络信息),侦听电话状态(呼叫状态服务状态、信号强度状态等)以及可以调用电话拨号器拨打电话!话不多开始本节内容~官方API:TelephonyManager1.获得TelephonyManager的服务对象TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);2.用法示例1)调用拨号器拨打电话号码Uri uri=Uri.parse("tel:"+电话号码); Intent intent=new Intent(Intent.ACTION_DIAL,uri); startActivity(intent); 2)获取Sim卡信息与网络信息运行效果图:实现代码:布局文件:activity_main.xml:<LinearLayout 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:orientation="vertical" android:padding="5dp" tools:context=".MainActivity"> <TextView android:id="@+id/tv_phone1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone3" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone4" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone5" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone6" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone7" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone8" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:id="@+id/tv_phone9" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /></LinearLayout>MainActivity.java:public class MainActivity extends AppCompatActivity { private TextView tv_phone1; private TextView tv_phone2; private TextView tv_phone3; private TextView tv_phone4; private TextView tv_phone5; private TextView tv_phone6; private TextView tv_phone7; private TextView tv_phone8; private TextView tv_phone9; private TelephonyManager tManager; private String[] phoneType = {"未知","2G","3G","4G"}; private String[] simState = {"状态未知","无SIM卡","被PIN加锁","被PUK加锁", "被NetWork PIN加锁","已准备好"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //①获得系统提供的TelphonyManager对象的实例 tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); bindViews(); } private void bindViews() { tv_phone1 = (TextView) findViewById(R.id.tv_phone1); tv_phone2 = (TextView) findViewById(R.id.tv_phone2); tv_phone3 = (TextView) findViewById(R.id.tv_phone3); tv_phone4 = (TextView) findViewById(R.id.tv_phone4); tv_phone5 = (TextView) findViewById(R.id.tv_phone5); tv_phone6 = (TextView) findViewById(R.id.tv_phone6); tv_phone7 = (TextView) findViewById(R.id.tv_phone7); tv_phone8 = (TextView) findViewById(R.id.tv_phone8); tv_phone9 = (TextView) findViewById(R.id.tv_phone9); tv_phone1.setText("设备编号:" + tManager.getDeviceId()); tv_phone2.setText("软件版本:" + (tManager.getDeviceSoftwareVersion()!= null? tManager.getDeviceSoftwareVersion():"未知")); tv_phone3.setText("运营商代号:" + tManager.getNetworkOperator()); tv_phone4.setText("运营商名称:" + tManager.getNetworkOperatorName()); tv_phone5.setText("网络类型:" + phoneType[tManager.getPhoneType()]); tv_phone6.setText("设备当前位置:" + (tManager.getCellLocation() != null ? tManager .getCellLocation().toString() : "未知位置")); tv_phone7.setText("SIM卡的国别:" + tManager.getSimCountryIso()); tv_phone8.setText("SIM卡序列号:" + tManager.getSimSerialNumber()); tv_phone9.setText("SIM卡状态:" + simState[tManager.getSimState()]); }}对了,别忘了在AndroidManifest.xml中加上权限哦!<!-- 添加访问手机位置的权限

[1] [2] [3]  下一页


10.1 TelephonyManager(电话管理器)