自动接听与挂断调用关系如下
1. 首先在启动apk初始界面的activity中,启动service服务,开始监听通话状态(InCallService)
//启动通话状态监听服务
private void startCallListen() {
Intent intent = new Intent(CallAndMessageEntranceActivity.this, InCallService.class);
startService(intent);
}
2. 在通话监听服务中,监听通话状态,当通话状态为CALL_STATE_RINGING时处理通话。
public class InCallService extends Service {
private TelecomManager mTelecomManager;
private TelephonyManager mTelephonymanager;
MyPhoneStateListener mMyPhoneStateListener = new MyPhoneStateListener();
@Override
public void onCreate() {
Log.e("InCallService", "onCreate()");
super.onCreate();
//获取电话服务
mTelecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mTelephonymanager == null) {
mTelephonymanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
}
mTelephonymanager.listen(mMyPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
return super.onStartCommand(intent, flags, startId);
}
//监听通过话
public class MyPhoneStateListener extends PhoneStateListener {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onCallStateChanged(int state, final String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//通话空闲状态
break;
case TelephonyManager.CALL_STATE_RINGING:
//通话来电状态
try {
Log.e("C_M_BlackCallService", "Monitor incoming calls");
//拦截来电判断来电时黑名单or白名单
splitWhiteAndBlack(incomingNumber);
} catch (Exception e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//电话挂起状态
break;
}
}
}
//incomingNumber:来电号码 依据来电号码判断黑白名单
private void splitWhiteAndBlack(String incomingNumber) {
//1.黑名单自动挂断
mTelecomManager.endCall();
//2.白名单自动接听
mTelecomManager.acceptRingingCall();
}
@Override
public void onDestroy() {
super.onDestroy();
}
到此这篇关于Android自动拦截与接听功能APK黑白名单的文章就介绍到这了,更多相关Android拦截与接听内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!