先去官网下载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();
}
}
}
}