安卓端黑名单拦截电话

安卓端黑名单拦截电话

需求:

当点击拦截按钮时会开启拦截服务,当点解取消拦截时则会关闭拦截服务

在xml文件中编写两个BUtton按钮,并设置其监听事件

在mainActivity中处理监听事件分别开启服务和关闭服务

public void start (View v) {
        startService(new Intent(this, BlackService.class));
    }

    public void end (View  v) {
        stopService(new Intent(this,BlackService.class));
    }
需要创建一个service(注意在xml文件进行配置)

package shangguigu.java.endcall;

import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class BlackService extends Service {

    private TelephonyManager tm;
    private PhoneStateListener listener = new PhoneStateListener() {

        // 当通话状态发生改变时调用
      
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:// 空闲 (挂断电话/未来电之前)
                    Log.e("TAG", "空闲 (挂断电话/未来电之前)");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:// 响铃
                    Log.e("TAG", "响铃");
                    // 如果来电电话是黑名单号(110), 就挂断电话
                    if ("15738517961".equals(incomingNumber)) {
                        try {
                            endCall();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:// 接通
                    Log.e("TAG", "接通");

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 挂断电话
     * @throws Exception
     */
    private void endCall() throws Exception {
        // 通过反射调用隐藏的API
        // 得到隐藏类的Class对象
        Class c = Class.forName("android.os.ServiceManager");
        // 得到方法所对应的Method对象
        Method method = c.getMethod("getService", String.class);
        // 调用方法
        IBinder iBinder = (IBinder) method.invoke(null,
                Context.TELEPHONY_SERVICE);
        // 得到接口对象
        ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
        // 结束通话
        telephony.endCall();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG", "Service onCreate()");

        // 得到电话管理器
        tm = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        // 监听电话状态
        tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "Service onDestroy()");
        // 停止电话监听
        tm.listen(listener, PhoneStateListener.LISTEN_NONE);
    }

}
在挂断电话的方法里需要使用系统的ITelephony。aidl文件ITelephony.aidl文件的包名需要与服务器的包名一致,android studio需要运行aidl文件

最后需要在xml里加权限

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

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值