andrid之来电自动挂断电话

先去官网下载ITelephony.aidl文件


然后在新建一个AIDL文件名字就叫ITelephony


在把下载的ITelephony.aidl替换你新建的ITelephony


重新编译


然后新建一个类来实现BroadcastReceiver


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

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

/**
 * Created by YaozzzzMABY on 2017/2/12.
 */

public class MyPhoneState extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.PHONE_STATE".equals(intent.getAction())) {
            //获取电话号码
            String number = intent.getStringExtra("incoming_number");
            Log.i("test", "有电话进来了," + number);
            Toast.makeText(context, "电话进来了:" + number, Toast.LENGTH_SHORT).show();

            //获取电话状态
            //电话管理者
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
                Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;//得到TelephonyManager的CLASS对象

                Method method = telephonyManagerClass.getDeclaredMethod("getITelephony", null);//得到TelephonyManager.getITelephony方法的对象method 

                method.setAccessible(true);//可以访问私有方法
                ITelephony telephony = (ITelephony) method.invoke(tm, null);//调用getITelephony方法发挥ITelephony对象

                telephony.endCall();//挂断电话
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值