java代码设置手机来电黑名单自动挂断

今天讲的是电话的黑名单自动挂断的功能,所以现在跟着我的步骤来吧!

一、首先切换到project区间去,在main的文件夹里面创一个aidl文件夹,把官网中下的两个文件夹cope到这个aidl文件夹里面去,再重新编译两次,第一次会报错,不用管,再编译一次就这可以,编译两次之后,在java类中写一个ITlelphony的类能够出现就算是成功了。




二、之后就需要在AndroidManifest加上权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

三、这部分就直接是java代码了,所以在这里我就直接上代码了

package com.zking.laci.android21_phone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by Laci on 2017/7/11.
 */

public class MyPhoneState extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        if("android.intent.action.PHONE_STATE".equals(intent.getAction())){
            //得到电话的管理者
            TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            //得到电话的状态
            int state=telephonyManager.getCallState();
            String number=intent.getStringExtra("incoming_number");
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i("test","来电了"+number);
                    //得到电话管理者的类
                    Class<TelephonyManager> clazz= TelephonyManager.class;
                    try {
                        //得到方法
                        Method method=clazz.getDeclaredMethod("getITelephony",null);
                        //设置可访问
                        method.setAccessible(true);
                        try {
                            ITelephony iTelephony= (ITelephony) method.invoke(telephonyManager,null);
                            //判断
                            if("18******882".equals(number)){//电话自己判断,我这里是举例子
                                try {
                                    iTelephony.endCall();//挂断
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i("test","接听了,通话中。。。录音中");
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.i("test","挂断了");
                    break;
            }
        }
    }
}

这样子,电话的黑名单就可以了,是不是觉得简单啊!



发布了114 篇原创文章 · 获赞 52 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/pang_ping/article/details/75194551
今日推荐