Android默认没有提供挂断/接听电话的api,需要伪装com/android/internal/telephony/ITelephony.aidl的接口来欺骗系统。而自动重复拨号可以通过(BroadcastReceiver)监听电话状态(android.intent.action.PHONE_STATE)来实现。
1、Android挂断和接听电话的接口
publicstaticvoidendCall(Contextcx){//挂断电话
TelephonyManager telMag=(TelephonyManager)cx
.getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager>c=TelephonyManager.class;
MethodmthEndCall=null;
try{
mthEndCall=c.getDeclaredMethod("getITelephony",(Class[])null);
mthEndCall.setAccessible(true);
ITelephony iTel=(ITelephony)mthEndCall.invoke(telMag,
(Object[])null);
iTel.endCall();
}catch(Exceptione){
e.printStackTrace();
}
}
privatevoidanswerCall(Contextcx){//接听电话
TelephonyManager telMag=(TelephonyManager)cx
.getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager>c=TelephonyManager.class;
MethodmthEndCall=null;
try{
mthEndCall=c.getDeclaredMethod("getITelephony",(Class[])null);
mthEndCall.setAccessible(true);
ITelephony iTel=(ITelephony)mthEndCall.invoke(telMag,
(Object[])null);
iTel.answerRingingCall();
}catch(Exceptione){
e.printStackTrace();
}
}
com/android/internal/telephony/ITelephony.aidl文件
packagecom.android.internal.telephony;
interfaceITelephony{
booleanendCall();
voidanswerRingingCall();
}
<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 cent.ticketcall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import .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相关配置:
<receiverandroid:name="PhoneStateReceiver">
<intent-filter>
<actionandroid:name="android.intent.action.PHONE_STATE"/>
<actionandroid:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
如果觉得《Android(AIDL)自动重复拨号及挂断/接听电话》对你有帮助,请点赞、收藏,并留下你的观点哦!