通过aidl及反射实现挂断电话
具体分三步:
(1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件(构建后在gen下有ITelephony.java文件,这是aidl生成的接口),文件内容如下:
package com.android.internal.telephony;
interface ITelephony{
boolean endCall();
void answerRingingCall();
}
(2)在需要的类中添加如下方法,代码如下(通过反射获取电话接口的实例)

/**
* @param context
* @return
*/
private static ITelephony getITelephony(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager) context
.getSystemService(TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
getITelephonyMethod = c.getDeclaredMethod("getITelephony",
(Class[]) null); // 获取声明的方法
getITelephonyMethod.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

try {
ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(
mTelephonyManager, (Object[]) null); // 获取实例
return iTelephony;
} catch (Exception e) {
e.printStackTrace();
}
return iTelephony;
}

(3)在来电时调用此实例,然后调用此endCall()方法。

mTelephonyManager = (TelephonyManager) this
.getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_STATE);

//电话实例
PhoneStateListener phoneStateListener = new PhoneStateListener() {

@Override
public void onCallStateChanged(int state, String incomingNumber) {

switch (state) {
case TelephonyManager.CALL_STATE_RINGING :
iTelephony = getITelephony(getApplicationContext()); //获取电话接口
if (iTelephony != null) {
try {
iTelephony.endCall(); // 挂断电话
Toast.makeText(getApplicationContext(),
"endCall "+ incomingNumber +" successful!", 3000).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
break;
default :
break;
}
}

};

//注意:在功能清单文件中添加电话的权限:

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

更多相关文章

  1. Android(安卓)豆瓣
  2. 获取网络信息,ip,子网掩码,网关,dns
  3. Android(安卓)notification的使用实例
  4. android实现短信监听
  5. Android(安卓)获取当前连接的wifi名称和rssi
  6. Android(安卓)上网流量监控方法
  7. Android(安卓)反编译APK详解
  8. Android之WebView和js交互-调用失败的看这里
  9. Android(安卓)8.0以后获取SSID为的解决方式

随机推荐

  1. android中图片倒影、圆角效果重绘
  2. Android全屏显示 无标题栏、全屏、设置为
  3. Android(安卓)Adapter
  4. Android(安卓)Settings中快速搜索流程
  5. SAX解析XML文件
  6. android 学习五 设置应用程序全屏(没有状
  7. Android(安卓)Android使用JSON与服务器交
  8. 修改 android版本 签名问题
  9. Android(安卓)keyevent 中的各个值
  10. Android(安卓)getDecorView用途——屏幕