这篇文章给大家分享的是有关如何实现Android自动拦截与接听功能APK黑白名单的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
自动接听与挂断调用关系如下
首先在启动apk初始界面的activity中,启动service服务,开始监听通话状态(InCallService)
//启动通话状态监听服务 private void startCallListen() { Intent intent = new Intent(CallAndMessageEntranceActivity.this, InCallService.class); startService(intent);}
在通话监听服务中,监听通话状态,当通话状态为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黑白名单”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!