TelephonyManager
概述
顾名思义,电话管理器,TelephonyManager类提供了对设备上的电话服务信息的访问。
可以使用这个类中的方法来完成如下工作:
- 确定电话服务和状态
- 访问某些类型的用户信息
- 注册一个监听程序来接收通知的电话状态变化
注意:你不能直接实例化这个类,需要通过Context.getSystemService(Context.TELEPHONY_SERVICE)获取服务对象
常用API方法
提供一个Demo,可以参考System.out内的输出内容——即常用的API方法。
需要一定的权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
package com.example.a4_8telephonymanager;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testTelephonyManager();
}
//电话服务管理API的方法
private void testTelephonyManager() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
System.out.println("电话状态:" + tm.getCallState());
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
System.out.println("唯一的设备 = " + tm.getDeviceId());
System.out.println("设备的软件版本号 = "+tm.getDeviceSoftwareVersion());
System.out.println("手机号 = "+tm.getLine1Number());
System.out.println("获取ISO标准的国家码,即国际长途区号 = "+tm.getNetworkCountryIso());
System.out.println("当前使用的网络类型 = "+tm.getNetworkType());
System.out.println("手机类型 = "+tm.getPhoneType());
System.out.println("SIM的状态信息 = "+tm.getSimState());
System.out.println("唯一的用户ID = "+tm.getSubscriberId());
System.out.println("SIM卡的序列号 = "+tm.getSimSerialNumber());
System.out.println("服务商名称 = "+tm.getSimOperatorName());
}
}
效果如图:
***听
提供一个Demo,需要注册一个***听器并调用。
package com.example.a4_8telephonymanager;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testTelephonyManager();
}
//电话服务管理API的方法
private void testTelephonyManager() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
System.out.println("电话状态:" + tm.getCallState());
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
System.out.println("唯一的设备 = " + tm.getDeviceId());
System.out.println("设备的软件版本号 = "+tm.getDeviceSoftwareVersion());
System.out.println("手机号 = "+tm.getLine1Number());
System.out.println("获取ISO标准的国家码,即国际长途区号 = "+tm.getNetworkCountryIso());
System.out.println("当前使用的网络类型 = "+tm.getNetworkType());
System.out.println("手机类型 = "+tm.getPhoneType());
System.out.println("SIM的状态信息 = "+tm.getSimState());
System.out.println("唯一的用户ID = "+tm.getSubscriberId());
System.out.println("SIM卡的序列号 = "+tm.getSimSerialNumber());
System.out.println("服务商名称 = "+tm.getSimOperatorName());
tm.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);
}
//电话服务***
private static class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
switch (state){
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("响铃状态");
break;
case TelephonyManager.CALL_STATE_IDLE:
System.out.println("挂机状态");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("接听状态");
break;
}
}
}
}
效果如下:
监听来电显示
需要三个权限,从上到下分别为:读取电话状态、开机启动、悬浮窗口
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
借助一个广播接收器完成,MainActivity只需要补上一句发送广播即可
//来电——发送一个广播
sendBroadcast(new Intent(this,PhoneListenerReceiver.class));
广播接收器的一些常规步骤不能省
完整代码如下:
package com.example.a4_8telephonymanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.TextView;
public class PhoneListenerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//获取电话管理器对象,并注册***听器
TelephonyManager tm= (TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new MyphoneStateLinstener(context),PhoneStateListener.LISTEN_CALL_STATE);
System.out.println("***听器注册成功");
}
//声明窗体管理器
static WindowManager wm = null;
private class MyphoneStateLinstener extends PhoneStateListener{
private Context context;
//来电显示——文字部分
TextView textView=null;
public MyphoneStateLinstener(Context context) {
this.context=context;
}
@Override
public void onCallStateChanged(int state, String phoneNumber) {
super.onCallStateChanged(state, phoneNumber);
//处于响铃状态
if (state == TelephonyManager.CALL_STATE_RINGING) {
//获取WindowManager对象
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
//设置为一个浮动层
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
//设置为不能触摸和没有焦点
params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
textView = new TextView(context);
textView.setText("当前来电号码为:" + phoneNumber);
//添加到浮动层
wm.addView(textView, params); }
//如果处于挂机状态,置空,回收
else if (state == TelephonyManager.CALL_STATE_IDLE) {
if (wm != null) {
wm.removeView(textView);
wm = null;
}
}
}
}
}
效果如👈