Android默认没有提供挂断/接听电话的api,需要伪装com/android/internal/telephony/ITelephony.aidl的接口来欺骗系统。而自动重复拨号可以通过(BroadcastReceiver)监听电话状态(android.intent.action.PHONE_STATE)来实现。
1、Android挂断和接听电话的接口
public static void endCall(Context cx) { //挂断电话
TelephonyManager telMag = (TelephonyManager) cx
.getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method mthEndCall = null;
try {
mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
mthEndCall.setAccessible(true);
ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
(Object[]) null);
iTel.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
private void answerCall(Context cx) { //接听电话
TelephonyManager telMag = (TelephonyManager) cx
.getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method mthEndCall = null;
try {
mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
mthEndCall.setAccessible(true);
ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
(Object[]) null);
iTel.answerRingingCall();
} catch (Exception e) {
e.printStackTrace();
}
}
com/android/internal/telephony/ITelephony.aidl文件
package com.android.internal.telephony;
interface ITelephony{
boolean endCall();
void answerRingingCall();
}
<strong>2、AndroidManifest.xml相关权限</strong>
<code lang='xml' width='auto' height='auto'>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
3、Android监听电话状态实现自动拨号
package com.orgcent.ticketcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
if(null != BaseAppliction.phone_num) {
BaseAppliction app = (BaseAppliction) context.getApplicationContext();
app.showCtrlWin();
}
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
if(null != BaseAppliction.phone_num) {
Intent it = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + BaseAppliction.phone_num));
it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
BaseAppliction.mCallCount ++;
BaseAppliction app = (BaseAppliction) context.getApplicationContext();
app.updateState();
}
break;
}
}
}
}
AndroidManifest.xml相关配置:
<receiver android:name="PhoneStateReceiver" >
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>