从零开始水安卓——电话服务

TelephonyManager

概述

顾名思义,电话管理器,TelephonyManager类提供了对设备上的电话服务信息的访问。

可以使用这个类中的方法来完成如下工作:

  1. 确定电话服务和状态
  2. 访问某些类型的用户信息
  3. 注册一个监听程序来接收通知的电话状态变化

注意:你不能直接实例化这个类,需要通过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;
                }
            }
        }
    }
}

效果如👈

全部评论

相关推荐

05-04 09:38
已编辑
门头沟学院 引擎开发
个人9本海硕,本硕期间一直在投游戏相关实习/校招,岗位由客户端-&gt;引擎-&gt;TA-&gt;AIGC。最终目标肯定是独游制作人,所以程序策划美术都点了些,感觉也没谁了。值此春招末尾总结下技术向校招要点,算是回馈牛客社区了。也附上我的Github和个人博客,欢迎各种交流讨论。&nbsp;前言&nbsp;首先是个人惯例的劝退游戏行业。参见缅怀故人&nbsp;和永远有多远&nbsp;,相比于互联网,游戏薪资大概相当但要求更高,加班严重且更为局限。如果你只是带着一腔热情想入这行,建议先找个日常实习了解下真实的游戏行业再做选择。&nbsp;准备&nbsp;当然,在你决定踏出这步后,第一步就是准备相关的笔试面试。这里先建议找到你感兴趣的公司岗位的JD,然后...
牛客28967172...:说的还是有道理的,我校招时就拿到过网易雷火好几个顶级项目组方向的offer,基本上流程和你说的一样。 但本质还是劝退互联网的游戏方向,本质上是代价更高,而且职业生涯容错率很低,方向比较窄。 代价是众所周知的严重加班,游戏大版本赶工基本上通宵无休,甚至国庆五一都没放假是常态。 职业生涯性价比低是因为游戏行业本质上就是赢家通吃,但你要跳槽只有腾讯网易等头部,要么就是米哈游莉莉丝库洛三七等少数中厂,然后就没了,公司是断崖的少 游戏开发相比互联网方向岗位非常非常少,比如网易整个雷火也才五六百人,里面十几个工作室,招人比例非常低,其他游戏公司也是一样。 而且方向也很窄,你做引擎开发就只能跳相关,你做游戏客户端也只能跳相关(游戏客户端都算吃香的,但市场hc也非常非常少,跳槽机会更少),基本上很难转回互联网 这里对比传统互联网,大厂多的都说不过来,而且容错率很大,你做搜索方向可以跳推荐,你做推荐方向可以跳广告,要求远没有游戏行业那么严,甚至你之前干测试都能跳槽研发方向
我的求职进度条
点赞 评论 收藏
分享
点赞 评论 收藏
分享
站队站对牛:进度也算很慢的了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务